package application.controller;

import application.externalServices.YahooTermExtractor;
import application.externalServices.DiscoWordnetService;

import application.model.EntitySearchResults;
import application.model.GraphEntity;
import application.model.Blacklist;
import application.model.EntityAccomplishment;
import application.model.EntityLookUp;

import application.model.GraphEntityAccomps;
import application.model.GraphEntityKeywords;

import application.service.BlacklistService;
import application.service.EntityAccomplishmentService;
import application.service.EntitySearchResultsService;


import application.form.SearchForm;
import application.form.SelectAnEntityForm;
import application.model.DistanceCalcSettings;
import application.service.DistanceCalcSettingsService;

import application.tools.FormatTools;
import application.tools.ReportTool;
import application.tools.ConflationTool;
import application.tools.Inflector;
import application.tools.UEALite;

import application.validator.SearchFormValidator;

import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView; // use servlet to have ModelAndView.addObject enabled
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@SessionAttributes
public class SearchFormController {

    @Autowired
    private EntityAccomplishmentService entityAccomplishmentService;
    @Autowired
    private EntitySearchResultsService entitySearchResultsService;
    @Autowired
    private BlacklistService blacklistService;
    @Autowired
    private DistanceCalcSettingsService distanceCalcSettingsService;
    //to be moved to another class
    private double tmpHighestWeight = 0.0;

    //Remove when XML code is moved to a seperate class
    //private String entityAdditionalDataXML = "";
    @RequestMapping(value = "/startSearch", method = RequestMethod.POST)
    public String startSearch(@ModelAttribute("searchForm") SearchForm searchForm, BindingResult result) {
        /*
        /////System.out.println("First Name:" + contact.getFirstname() +
        "Last Name:" + contact.getLastname());

         */
        return "redirect:connectapp.html";
    }

    @RequestMapping(value = "/connectapp", method = RequestMethod.GET)
    public ModelAndView showSearch() {

        return new ModelAndView("connectapp", "command", new SearchForm()).addObject("searchresults", "").addObject("visualDataSource", "''");
    }

    /*
     * This is for Term (Keyword) search.  Default option.
     */
    //TODO: Validate the input variables
    @RequestMapping(value = "/connectapp", method = RequestMethod.POST)
    public ModelAndView showSearchAndResults(@ModelAttribute("connectapp") SearchForm searchForm, BindingResult result) {

        List searchResults = new ArrayList();  //list of query results
        String visualDataSource = ""; // xml string input for visula graph
        String searchString = "";  // combined search input
        HashMap<String, String> validatedFormValues = new HashMap<String, String>(); //contains validated input values
        HashMap<String, String> centralNode = new HashMap<String, String>();  //holds name and id of central graph node for use in report creation

        SearchFormValidator sfv = new SearchFormValidator(); //input validation tool

        //validate input        
        validatedFormValues.put("searchOperator", sfv.validateOperator(searchForm.getSearchOperator()));

        //check if need to search with an opperator (and/not/none)
        if (validatedFormValues.get("searchOperator").toLowerCase().equals("none")) {
            validatedFormValues.put("searchTerm1", sfv.validateSearchTerm(searchForm.getSearchTerm1()));
            //validatedFormValues.put("searchOperator", sfv.validateOperator(searchForm.getSearchOperator()));
            validatedFormValues.put("searchType", sfv.validateSearchType(searchForm.getSearchTypeRadioB()));
            //Note: sfv.validateSynonyms returns a string not a list
            validatedFormValues.put("searchTerm1Synonyms", sfv.validateSynonyms(searchForm.getSearchTerm1Synonyms()));
            validatedFormValues.put("searchTerm2Synonyms", "");
            searchString = validatedFormValues.get("searchTerm1");

            List<DistanceCalcSettings> distanceCalcSettings = distanceCalcSettingsService.getAllDistanceCalcSettings();
            //new code v2

            HashMap<String, String> validatedFormValuesPerSearch = new HashMap<String, String>(); //contains validated input values
            HashMap<Long, GraphEntity> searchResults2graphEntity = new HashMap<Long, GraphEntity>();
            //EntityID, GraphEntityKeywords
            HashMap<Long, GraphEntityKeywords> searchResults2graphEntityKeywords = new HashMap<Long, GraphEntityKeywords>();
            //EntityID, GraphEntityAccomps
            HashMap<Long, GraphEntityAccomps> searchResults2graphEntityAccomps = new HashMap<Long, GraphEntityAccomps>();

            ArrayList<String> allSearchTerms1 = new ArrayList<String>(); //holds all search terms linked to term1 (inc synonyms)

            //add all term1 search terms (inc. searchTerm1 field and any synonyms) to an array
            allSearchTerms1.add(validatedFormValues.get("searchTerm1"));
            if (validatedFormValues.get("searchTerm1Synonyms") != null) {
                if (validatedFormValues.get("searchTerm1Synonyms").trim().length() >= 1) {
                    String[] synonyms1 = validatedFormValues.get("searchTerm1Synonyms").split(";;");
                    for (int i = 0; synonyms1.length > i; i++) {
                        allSearchTerms1.add(synonyms1[i]);
                    }
                }
            }

            //clear sysnonyms vars as not needed anymore in v2
            validatedFormValuesPerSearch.put("searchTerm1Synonyms", "");
            validatedFormValuesPerSearch.put("searchOperator", validatedFormValues.get("searchOperator"));

            //loop each keyword combination from searchTerm 1 and 2 & sysnonyms 1 and 2
            //check if synonyms1 exist
            if (allSearchTerms1.size() >= 1) {

                for (int x = 0; allSearchTerms1.size() > x; x++) {

                    validatedFormValuesPerSearch.put("searchTerm1", allSearchTerms1.get(x));
                    validatedFormValuesPerSearch.put("searchTerm2", "");
                    List<EntitySearchResults> esr1 = entitySearchResultsService.listSelectedEntitySearchResults(validatedFormValuesPerSearch);

                    //report tool
                    FormatTools ft = new FormatTools();
                    ft.entSearchRes2EntObjList2(esr1, allSearchTerms1.get(x), null, distanceCalcSettings);
                    //add entity details to master list
                    for (Long entID : ft.getSearchResults2ge().keySet()) {
                        if (searchResults2graphEntity.containsKey(entID)) {
                            searchResults2graphEntity.get(entID).setTotalWeight(searchResults2graphEntity.get(entID).getTotalWeight() + ft.getSearchResults2ge().get(entID).getTotalWeight());
                        } else {
                            searchResults2graphEntity.put(entID, ft.getSearchResults2ge().get(entID));
                        }
                    }
                    //add keyword details to master list
                    String tmpSearchTerm = allSearchTerms1.get(x);
                    for (Long entID : ft.getSearchResults2geks().keySet()) {
                        if (searchResults2graphEntityKeywords.containsKey(entID)) {
                            //if keyword already exists
                            if (searchResults2graphEntityKeywords.get(entID).getGek().containsKey(tmpSearchTerm)) {
                                searchResults2graphEntityKeywords.get(entID).getGek().get(tmpSearchTerm).addAll(ft.getSearchResults2geks().get(entID).getGek().get(tmpSearchTerm));
                            } else {
                                searchResults2graphEntityKeywords.get(entID).getGek().put(tmpSearchTerm, ft.getSearchResults2geks().get(entID).getGek().get(tmpSearchTerm));
                            }
                        } else {
                            searchResults2graphEntityKeywords.put(entID, ft.getSearchResults2geks().get(entID));
                        }
                    }
                    //add entity accomplishment details to master list
                    for (Long entID2 : ft.getSearchResults2geas().keySet()) {
                        if (searchResults2graphEntityAccomps.containsKey(entID2)) {
                            for (Long tmpEntAccompId : ft.getSearchResults2geas().get(entID2).getGea().keySet()) {
                                if (searchResults2graphEntityAccomps.get(entID2).getGea().containsKey(tmpEntAccompId)) {
                                } else {
                                    searchResults2graphEntityAccomps.get(entID2).getGea().put(tmpEntAccompId, ft.getSearchResults2geas().get(entID2).getGea().get(tmpEntAccompId));
                                }
                            }
                        } else {
                            searchResults2graphEntityAccomps.put(entID2, ft.getSearchResults2geas().get(entID2));
                        }
                    }

                    /*end search report*/
                    searchResults.addAll(esr1);
                }
            }

            //visualDataSource = this.visualDataSource(searchResults, searchString);
            visualDataSource = this.visualDataSourceKwrdBased(searchResults, searchString, searchResults2graphEntity);

            ReportTool repTool = new ReportTool();
            centralNode.put("name", searchString);
            centralNode.put("id", "001");
            String reportCSV = repTool.createSearchReportAndDetailsXML(searchResults2graphEntity, searchResults2graphEntityAccomps, searchResults2graphEntityKeywords, validatedFormValues.get("searchTerm1"), centralNode);
            String additionalDataXML = repTool.getEntityAdditionalDataXML();
            return new ModelAndView("connectapp", "command", new SearchForm()).addObject("searchresults", searchResults).addObject("visualDataSource", visualDataSource) //.addObject("entityAdditionalDataXML", this.getEntityAdditionalDataXML())
                    .addObject("reportDataSource", reportCSV).addObject("entityAdditionalDataXML", additionalDataXML).addObject("highestWeightValue", this.tmpHighestWeight);

            //end new code v2

        } else if (validatedFormValues.get("searchOperator").toLowerCase().equals("not")) {
            validatedFormValues.put("searchTerm1", sfv.validateSearchTerm(searchForm.getSearchTerm1()));
            validatedFormValues.put("searchTerm2", sfv.validateSearchTerm(searchForm.getSearchTerm2()));
            validatedFormValues.put("searchType", sfv.validateSearchType(searchForm.getSearchTypeRadioB()));
            //Note: sfv.validateSynonyms returns a string not a list
            validatedFormValues.put("searchTerm1Synonyms", sfv.validateSynonyms(searchForm.getSearchTerm1Synonyms()));
            validatedFormValues.put("searchTerm2Synonyms", sfv.validateSynonyms(searchForm.getSearchTerm2Synonyms()));
            searchString = validatedFormValues.get("searchTerm1") + " " + validatedFormValues.get("searchOperator") + " " + validatedFormValues.get("searchTerm2");
            //do loop keyword search here
            HashMap<String, HashMap<String, String>> emptyVar = new HashMap<String, HashMap<String, String>>();
            HashMap<String, String> validatedFormValuesPerSearch = new HashMap<String, String>(); //contains validated input values
            HashMap<Long, GraphEntity> searchResults2graphEntity = new HashMap<Long, GraphEntity>();
            //EntityID, GraphEntityKeywords
            HashMap<Long, GraphEntityKeywords> searchResults2graphEntityKeywords = new HashMap<Long, GraphEntityKeywords>();
            //EntityID, GraphEntityAccomps
            HashMap<Long, GraphEntityAccomps> searchResults2graphEntityAccomps = new HashMap<Long, GraphEntityAccomps>();

            //formated search results - entity keyed hashmap
            //HashMap<String, GraphEntity> formatedSearchResults = new HashMap<String, GraphEntity>();

            if (sfv.getInputValidationErrorList().size() > 0) {
                //visualDataSource = this.visualDataSource(searchResults, searchString);
                visualDataSource = this.visualDataSourceEntityBased(searchResults, searchString, emptyVar, null, null);
            } else {
                validatedFormValuesPerSearch.put("searchOperator", validatedFormValues.get("searchOperator"));
                validatedFormValuesPerSearch.put("searchType", validatedFormValues.get("searchType"));
                validatedFormValuesPerSearch.put("searchTerm1Synonyms", validatedFormValues.get("searchTerm1Synonyms"));
                validatedFormValuesPerSearch.put("searchTerm2Synonyms", validatedFormValues.get("searchTerm2Synonyms"));

                ArrayList<String> allSearchTerms1 = new ArrayList<String>(); //holds all search terms linked to term1 (inc synonyms)
                ArrayList<String> allSearchTerms2 = new ArrayList<String>(); //holds all search terms linked to term2 (inc synonyms)

                List<DistanceCalcSettings> distanceCalcSettings = distanceCalcSettingsService.getAllDistanceCalcSettings();

                //add all term1 search terms (inc. searchTerm1 field and any synonyms) to an array
                allSearchTerms1.add(validatedFormValues.get("searchTerm1"));
                if (validatedFormValuesPerSearch.get("searchTerm1Synonyms").trim().length() >= 1) {
                    String[] synonyms1 = validatedFormValuesPerSearch.get("searchTerm1Synonyms").split(";;");
                    for (int i = 0; synonyms1.length > i; i++) {
                        allSearchTerms1.add(synonyms1[i]);
                    }
                }

                //add all term2 search terms (inc. searchTerm2 field and any synonyms) to an array
                allSearchTerms2.add(validatedFormValues.get("searchTerm2"));
                if (validatedFormValuesPerSearch.get("searchTerm2Synonyms").trim().length() >= 1) {
                    String[] synonyms2 = validatedFormValuesPerSearch.get("searchTerm2Synonyms").split(";;");
                    for (int j = 0; synonyms2.length > j; j++) {
                        allSearchTerms2.add(synonyms2[j]);
                    }
                }

                //clear sysnonyms vars as not needed anymore in v2
                validatedFormValuesPerSearch.put("searchTerm1Synonyms", "");
                validatedFormValuesPerSearch.put("searchTerm2Synonyms", "");

                //loop each keyword combination from searchTerm 1 and 2 & sysnonyms 1 and 2
                //check if synonyms1 exist
                if (allSearchTerms1.size() >= 1) {

                    for (int x = 0; allSearchTerms1.size() > x; x++) {

                        if (allSearchTerms2.size() >= 1) {
                            //now search for each combination replacing searchTerm1&2 values
                            //values with new combination pair from synonyms
                            for (int y = 0; allSearchTerms2.size() > y; y++) {
                                validatedFormValuesPerSearch.put("searchTerm1", allSearchTerms1.get(x));
                                validatedFormValuesPerSearch.put("searchTerm2", allSearchTerms2.get(y));
                                //search terms 1 and 2
                                //searchResults.addAll(entitySearchResultsService.listSelectedEntitySearchResults(validatedFormValuesPerSearch));
                                /*search report*/
                                List<EntitySearchResults> esr1 = entitySearchResultsService.listSelectedEntitySearchResults(validatedFormValuesPerSearch);
                                String tmpSearchTerm = allSearchTerms1.get(x) + " " + validatedFormValuesPerSearch.get("searchOperator") + " " + allSearchTerms2.get(y);

                                //report tool
                                FormatTools ft = new FormatTools();
                                //ft.entSearchRes2EntObjList(esr1, allSearchTerms1.get(x) + " " + validatedFormValues.get("searchOperator") + " " + allSearchTerms2.get(y));
                                ft.entSearchRes2EntObjList2(esr1, tmpSearchTerm, validatedFormValues.get("searchOperator"), distanceCalcSettings);
                                //add entity details to master list
                                for (Long entID : ft.getSearchResults2ge().keySet()) {
                                    if (searchResults2graphEntity.containsKey(entID)) {
                                        searchResults2graphEntity.get(entID).setTotalWeight(searchResults2graphEntity.get(entID).getTotalWeight() + ft.getSearchResults2ge().get(entID).getTotalWeight());
                                    } else {
                                        searchResults2graphEntity.put(entID, ft.getSearchResults2ge().get(entID));
                                    }
                                }
//System.out.println("******* the keyword is: "+tmpSearchTerm);
                                //add keyword details to master list                                
                                for (Long entID : ft.getSearchResults2geks().keySet()) {
                                    if (searchResults2graphEntityKeywords.containsKey(entID)) {
                                        //if keyword already exists
                                        if (searchResults2graphEntityKeywords.get(entID).getGek().containsKey(tmpSearchTerm)) {
                                            searchResults2graphEntityKeywords.get(entID).getGek().get(tmpSearchTerm).addAll(ft.getSearchResults2geks().get(entID).getGek().get(tmpSearchTerm));
                                        } else {
                                            searchResults2graphEntityKeywords.get(entID).getGek().put(tmpSearchTerm, ft.getSearchResults2geks().get(entID).getGek().get(tmpSearchTerm));
                                        }
                                    } else {
                                        searchResults2graphEntityKeywords.put(entID, ft.getSearchResults2geks().get(entID));
                                    }
                                }

                                //add entity accomplishment details to master list
                                for (Long entID2 : ft.getSearchResults2geas().keySet()) {
                                    if (searchResults2graphEntityAccomps.containsKey(entID2)) {
                                        for (Long tmpEntAccompId : ft.getSearchResults2geas().get(entID2).getGea().keySet()) {
                                            if (searchResults2graphEntityAccomps.get(entID2).getGea().containsKey(tmpEntAccompId)) {
                                            } else {
                                                searchResults2graphEntityAccomps.get(entID2).getGea().put(tmpEntAccompId, ft.getSearchResults2geas().get(entID2).getGea().get(tmpEntAccompId));
                                            }
                                        }
                                    } else {
                                        searchResults2graphEntityAccomps.put(entID2, ft.getSearchResults2geas().get(entID2));
                                    }
                                }

                                /*end search report*/
                                searchResults.addAll(esr1);
                            }
                        }
                    }
                }

                //visualDataSource = this.visualDataSource(searchResults, searchString);
                visualDataSource = this.visualDataSourceKwrdBased(searchResults, searchString, searchResults2graphEntity);
            }

            ReportTool repTool = new ReportTool();
            centralNode.put("name", searchString);
            centralNode.put("id", "001");
            String reportCSV = repTool.createSearchReportAndDetailsXML(searchResults2graphEntity, searchResults2graphEntityAccomps, searchResults2graphEntityKeywords, validatedFormValues.get("searchTerm1") + " NOT " + validatedFormValues.get("searchTerm2"), centralNode);
            String additionalDataXML = repTool.getEntityAdditionalDataXML();

            return new ModelAndView("connectapp", "command", new SearchForm()).addObject("searchresults", searchResults).addObject("visualDataSource", visualDataSource).addObject("entityAdditionalDataXML", additionalDataXML).addObject("reportDataSource", reportCSV).addObject("highestWeightValue", this.tmpHighestWeight);

        } else if (searchForm.getSearchOperator().toLowerCase().equals("and")) {
            validatedFormValues.put("searchTerm1", sfv.validateSearchTerm(searchForm.getSearchTerm1()));
            validatedFormValues.put("searchTerm2", sfv.validateSearchTerm(searchForm.getSearchTerm2()));
            validatedFormValues.put("searchType", sfv.validateSearchType(searchForm.getSearchTypeRadioB()));
            //Note: sfv.validateSynonyms returns a string not a list
            validatedFormValues.put("searchTerm1Synonyms", sfv.validateSynonyms(searchForm.getSearchTerm1Synonyms()));
            validatedFormValues.put("searchTerm2Synonyms", sfv.validateSynonyms(searchForm.getSearchTerm2Synonyms()));
            searchString = validatedFormValues.get("searchTerm1") + " " + validatedFormValues.get("searchOperator") + " " + validatedFormValues.get("searchTerm2");
            //do loop keyword search here
            HashMap<String, HashMap<String, String>> emptyVar = new HashMap<String, HashMap<String, String>>();
            HashMap<String, String> validatedFormValuesPerSearch = new HashMap<String, String>(); //contains validated input values
            //HashMap<String, GraphEntity> searchResults2graphEntity = new HashMap<String, GraphEntity>();
            //formated search results - entity keyed hashmap
            HashMap<Long, GraphEntity> searchResults2graphEntity = new HashMap<Long, GraphEntity>();
            //EntityID, GraphEntityKeywords
            HashMap<Long, GraphEntityKeywords> searchResults2graphEntityKeywords = new HashMap<Long, GraphEntityKeywords>();
            //EntityID, GraphEntityAccomps
            HashMap<Long, GraphEntityAccomps> searchResults2graphEntityAccomps = new HashMap<Long, GraphEntityAccomps>();


            if (sfv.getInputValidationErrorList().size() > 0) {
                //visualDataSource = this.visualDataSource(searchResults, searchString);
                visualDataSource = this.visualDataSourceEntityBased(searchResults, searchString, emptyVar, null, null);
            } else {
                validatedFormValuesPerSearch.put("searchOperator", validatedFormValues.get("searchOperator"));
                validatedFormValuesPerSearch.put("searchType", validatedFormValues.get("searchType"));
                validatedFormValuesPerSearch.put("searchTerm1Synonyms", validatedFormValues.get("searchTerm1Synonyms"));
                validatedFormValuesPerSearch.put("searchTerm2Synonyms", validatedFormValues.get("searchTerm2Synonyms"));

                ArrayList<String> allSearchTerms1 = new ArrayList<String>(); //holds all search terms linked to term1 (inc synonyms)
                ArrayList<String> allSearchTerms2 = new ArrayList<String>(); //holds all search terms linked to term2 (inc synonyms)

                List<DistanceCalcSettings> distanceCalcSettings = distanceCalcSettingsService.getAllDistanceCalcSettings();

                //add all term1 search terms (inc. searchTerm1 field and any synonyms) to an array
                allSearchTerms1.add(validatedFormValues.get("searchTerm1"));
                if (validatedFormValuesPerSearch.get("searchTerm1Synonyms").trim().length() >= 1) {
                    String[] synonyms1 = validatedFormValuesPerSearch.get("searchTerm1Synonyms").split(";;");
                    for (int i = 0; synonyms1.length > i; i++) {
                        allSearchTerms1.add(synonyms1[i]);
                    }
                }

                //add all term2 search terms (inc. searchTerm2 field and any synonyms) to an array
                allSearchTerms2.add(validatedFormValues.get("searchTerm2"));
                if (validatedFormValuesPerSearch.get("searchTerm2Synonyms").trim().length() >= 1) {
                    String[] synonyms2 = validatedFormValuesPerSearch.get("searchTerm2Synonyms").split(";;");
                    for (int j = 0; synonyms2.length > j; j++) {
                        allSearchTerms2.add(synonyms2[j]);
                    }
                }

                //clear sysnonyms vars as not needed anymore in v2
                validatedFormValuesPerSearch.put("searchTerm1Synonyms", "");
                validatedFormValuesPerSearch.put("searchTerm2Synonyms", "");

                //loop each keyword combination from searchTerm 1 and 2 & sysnonyms 1 and 2
                //check if synonyms1 exist
                if (allSearchTerms1.size() >= 1) {

                    for (int x = 0; allSearchTerms1.size() > x; x++) {

                        if (allSearchTerms2.size() >= 1) {
                            //now search for each combination replacing searchTerm1&2 values
                            //values with new combination pair from synonyms
                            for (int y = 0; allSearchTerms2.size() > y; y++) {
                                validatedFormValuesPerSearch.put("searchTerm1", allSearchTerms1.get(x));
                                validatedFormValuesPerSearch.put("searchTerm2", allSearchTerms2.get(y));
                                //search terms 1 and 2
                                //searchResults.addAll(entitySearchResultsService.listSelectedEntitySearchResults(validatedFormValuesPerSearch));
                                /*search report*/
                                List<EntitySearchResults> esr1 = entitySearchResultsService.listSelectedEntitySearchResults(validatedFormValuesPerSearch);

                                String tmpSearchTerm = allSearchTerms1.get(x) + " " + validatedFormValuesPerSearch.get("searchOperator") + " " + allSearchTerms2.get(y);

//System.out.println("&&&&&& term is : "+tmpSearchTerm+ "size is: "+esr1.size());
                                //report tool
                                FormatTools ft = new FormatTools();
                                //ft.entSearchRes2EntObjList(esr1, allSearchTerms1.get(x) + " " + validatedFormValues.get("searchOperator") + " " + allSearchTerms2.get(y));
                                ft.entSearchRes2EntObjList2(esr1, tmpSearchTerm, searchForm.getSearchOperator(), distanceCalcSettings);

                                //add entity details to master list
                                for (Long entID : ft.getSearchResults2ge().keySet()) {
                                    if (searchResults2graphEntity.containsKey(entID)) {
                                        searchResults2graphEntity.get(entID).setTotalWeight(searchResults2graphEntity.get(entID).getTotalWeight() + ft.getSearchResults2ge().get(entID).getTotalWeight());
                                    } else {
                                        searchResults2graphEntity.put(entID, ft.getSearchResults2ge().get(entID));
                                    }
                                }

                                //add keyword details to master list                                
                                for (Long entID : ft.getSearchResults2geks().keySet()) {
                                    if (searchResults2graphEntityKeywords.containsKey(entID)) {
                                        //if keyword already exists
                                        if (searchResults2graphEntityKeywords.get(entID).getGek().containsKey(tmpSearchTerm)) {
                                            searchResults2graphEntityKeywords.get(entID).getGek().get(tmpSearchTerm).addAll(ft.getSearchResults2geks().get(entID).getGek().get(tmpSearchTerm));
                                        } else {
                                            searchResults2graphEntityKeywords.get(entID).getGek().put(tmpSearchTerm, ft.getSearchResults2geks().get(entID).getGek().get(tmpSearchTerm));
                                        }
                                    } else {
                                        searchResults2graphEntityKeywords.put(entID, ft.getSearchResults2geks().get(entID));
                                    }
                                }
                                //add entity accomplishment details to master list
                                for (Long entID2 : ft.getSearchResults2geas().keySet()) {
                                    if (searchResults2graphEntityAccomps.containsKey(entID2)) {
                                        for (Long tmpEntAccompId : ft.getSearchResults2geas().get(entID2).getGea().keySet()) {
                                            if (searchResults2graphEntityAccomps.get(entID2).getGea().containsKey(tmpEntAccompId)) {
                                            } else {
                                                searchResults2graphEntityAccomps.get(entID2).getGea().put(tmpEntAccompId, ft.getSearchResults2geas().get(entID2).getGea().get(tmpEntAccompId));
                                            }
                                        }
                                    } else {
                                        searchResults2graphEntityAccomps.put(entID2, ft.getSearchResults2geas().get(entID2));
                                    }
                                }

                                /*end search report*/
                                searchResults.addAll(esr1);
                            }
                        }
                    }
                }

                //visualDataSource = this.visualDataSource(searchResults, searchString);
                visualDataSource = this.visualDataSourceKwrdBased(searchResults, searchString, searchResults2graphEntity);
            }

            ReportTool repTool = new ReportTool();
            centralNode.put("name", searchString);
            centralNode.put("id", "001");
            String reportCSV = repTool.createSearchReportAndDetailsXML(searchResults2graphEntity, searchResults2graphEntityAccomps, searchResults2graphEntityKeywords, validatedFormValues.get("searchTerm1") + " AND " + validatedFormValues.get("searchTerm2"), centralNode);
            String additionalDataXML = repTool.getEntityAdditionalDataXML();

            return new ModelAndView("connectapp", "command", new SearchForm()).addObject("searchresults", searchResults).addObject("visualDataSource", visualDataSource).addObject("entityAdditionalDataXML", additionalDataXML).addObject("reportDataSource", reportCSV).addObject("highestWeightValue", this.tmpHighestWeight);

        }

        HashMap<String, HashMap<String, String>> emptyVar = new HashMap<String, HashMap<String, String>>();

        if (sfv.getInputValidationErrorList().size() > 0) {
            //visualDataSource = this.visualDataSource(searchResults, searchString);
            visualDataSource = this.visualDataSourceEntityBased(searchResults, searchString, emptyVar, null, null);
        } else {
            searchResults = entitySearchResultsService.listSelectedEntitySearchResults(validatedFormValues);

            //visualDataSource = this.visualDataSource(searchResults, searchString);
            visualDataSource = this.visualDataSourceKwrdBased(searchResults, searchString, null);
        }

        return new ModelAndView("connectapp", "command", new SearchForm()).addObject("searchresults", searchResults).addObject("visualDataSource", visualDataSource).addObject("entityAdditionalDataXML", "").addObject("highestWeightValue", this.tmpHighestWeight);
        //return new ModelAndView("contact", "command", new Contact()).addObject("entityAccomp", entityAccomplishmentService.listSelectedEntityAccomp());
    }

    /*
     * This is for Entity (Person) search
     */
    //TODO: Entity (Person) search
    @RequestMapping(value = "/connectapp", method = RequestMethod.POST, params = "searchTypeRadioB=Person-Entity")
    public ModelAndView showSearchAndResultsEntity(@ModelAttribute("connectapp") SearchForm searchForm, BindingResult result) {

        SearchFormValidator sfv = new SearchFormValidator(); //input validation tool
        HashMap<String, String> validatedFormValues = new HashMap<String, String>(); //contains validated input values
        String visualDataSource = ""; // xml string input for visula graph

        //first find entity using input string
        validatedFormValues.put("searchTerm1", sfv.validateSearchTerm(searchForm.getSearchTerm1()));
        List searchResults = entitySearchResultsService.listEntityLookUpByName(validatedFormValues);
        ArrayList<EntitySearchResults> searchResultsMain = new ArrayList<EntitySearchResults>();
        //TODO: remove searchResultsTMP once scoring implemented (showSearchAndResultsEntity)
        ArrayList<EntitySearchResults> searchResultsTMP = new ArrayList<EntitySearchResults>();
        HashMap<String, List<EntitySearchResults>> searchResultsWithKW = new HashMap<String, List<EntitySearchResults>>();

        String pageToDisplay = "";

        Object formObject = new SearchForm();

        if (searchResults == null || searchResults.isEmpty()) {
            pageToDisplay = "selectAnEntity";
            formObject = new SelectAnEntityForm();
            return new ModelAndView(pageToDisplay, "command", formObject).addObject("errorMsg", "No match found, please try again").addObject("visualDataSource", visualDataSource).addObject("reportDataSource", "''");
        } else if (searchResults.size() > 1) {
            pageToDisplay = "selectAnEntity";
            formObject = new SelectAnEntityForm();
            return new ModelAndView(pageToDisplay, "command", formObject).addObject("searchresults", searchResults).addObject("visualDataSource", visualDataSource).addObject("reportDataSource", "''");
        } else {
            //if one result only
            EntityLookUp esr = (EntityLookUp) searchResults.get(0);
            //set central node info for report and details XML
            HashMap<String, String> centralNode = new HashMap<String, String>();
            centralNode.put("name", esr.getEntityFname() + " " + esr.getEntitySname());
            centralNode.put("id", esr.getEntityID().toString());

            YahooTermExtractor urlcn = new YahooTermExtractor();
            //EntityAttributes entsAndAttribs = new EntityAttributes();
            List<Blacklist> blacklistTerms = blacklistService.listAllBlacklistTerms();
            //HashMap<String, GraphEntity> searchResults2graphEntity = new HashMap<String, GraphEntity>();
            //formated search results - entity keyed hashmap
            HashMap<String, GraphEntity> formatedSearchResults = new HashMap<String, GraphEntity>();
            String formatedSearchReport = ""; //report content

            //formated search results - entity keyed hashmap
            HashMap<Long, GraphEntity> searchResults2graphEntity = new HashMap<Long, GraphEntity>();
            //EntityID, GraphEntityKeywords
            HashMap<Long, GraphEntityKeywords> searchResults2graphEntityKeywords = new HashMap<Long, GraphEntityKeywords>();
            //EntityID, GraphEntityAccomps
            HashMap<Long, GraphEntityAccomps> searchResults2graphEntityAccomps = new HashMap<Long, GraphEntityAccomps>();
            //list of searched keywords, eliminate duplicates
            ArrayList<String> alreadySearchedKeyTerms = new ArrayList<String>();

            //get persons accomplishments
            List<EntityAccomplishment> listOfEntityAccomplishments = entityAccomplishmentService.listSelectedEntityAccomp(esr.getEntityID().toString());
            //genereate keywords

            /////System.out.println("START " + listOfEntityAccomplishments.size());
            //then search for matching entities
            //Yahoo term extractor
            int countBlockItems = 0;
            int mainCount = 1;
            String blockOfFive = "";

            List<DistanceCalcSettings> distanceCalcSettings = distanceCalcSettingsService.getAllDistanceCalcSettings();
            ArrayList<String> shortDBKeyterms = new ArrayList<String>();

            for (int eaid = 0; eaid < listOfEntityAccomplishments.size(); eaid++) {
                //if (!alreadySearchedKeyTerms.contains(listOfEntityAccomplishments.get(eaid).getTitle().trim())) {
                //if keyterm already searched for then ignore it to avoid duplicate results and incorrect match score
                if (!searchResultsWithKW.containsKey(listOfEntityAccomplishments.get(eaid).getTitle().toLowerCase())) {
                    //System.out.println(">88888< " + listOfEntityAccomplishments.get(eaid).getTitle());
                    HashMap<String, String> keywordVal = new HashMap<String, String>();
                    keywordVal.put("searchTerm1", listOfEntityAccomplishments.get(eaid).getTitle());

                    //if (!blackList.contains(s.toLowerCase().trim()) && !s.trim().equals("") && s.length()>0) {

                    if (listOfEntityAccomplishments.size() == mainCount) {
                        countBlockItems = 4;
                    }
                    //QuerySet.getExpandQ1().add(new ExpandedKeyword(s));

                    if (listOfEntityAccomplishments.get(eaid).getTitle().split(" ").length > 2) {

                        if (countBlockItems < 5 && !listOfEntityAccomplishments.get(eaid).getTitle().equals("")) {
                            /*Use . to indicate end of sentance.  Yeahoo extractor works better.*/
                            blockOfFive = blockOfFive + ". " + listOfEntityAccomplishments.get(eaid).getTitle().replace('"', ' ');
                            countBlockItems++;
                        }

                        //Search keywords from database
                        //black list check not required since black list contains chort keywords
                        List<EntitySearchResults> esr1 = entitySearchResultsService.listSelectedEntityByIDResults(listOfEntityAccomplishments.get(eaid).getTitle().trim(), esr.getEntityID().toString());
                        /*search report*/
                        //report tool
                        FormatTools ft = new FormatTools();
                        ft.entSearchRes2EntObjList2(esr1, listOfEntityAccomplishments.get(eaid).getTitle().trim(), null, distanceCalcSettings);
                        //add entity details to master list
                        for (Long entID : ft.getSearchResults2ge().keySet()) {
                            if (searchResults2graphEntity.containsKey(entID)) {
                                searchResults2graphEntity.get(entID).setTotalWeight(searchResults2graphEntity.get(entID).getTotalWeight() + ft.getSearchResults2ge().get(entID).getTotalWeight());
//System.out.println("**** 1: "+entID+" - weight is :"+searchResults2graphEntity.get(entID).getTotalWeight());
                            } else {
                                searchResults2graphEntity.put(entID, ft.getSearchResults2ge().get(entID));
                            }
                        }
                        //add keyword details to master list
                        String tmpSearchTerm = listOfEntityAccomplishments.get(eaid).getTitle().trim();
                        for (Long entID : ft.getSearchResults2geks().keySet()) {
                            if (searchResults2graphEntityKeywords.containsKey(entID)) {
                                //if keyword already exists
                                if (searchResults2graphEntityKeywords.get(entID).getGek().containsKey(tmpSearchTerm)) {
                                    searchResults2graphEntityKeywords.get(entID).getGek().get(tmpSearchTerm).addAll(ft.getSearchResults2geks().get(entID).getGek().get(tmpSearchTerm));
                                } else {
                                    searchResults2graphEntityKeywords.get(entID).getGek().put(tmpSearchTerm, ft.getSearchResults2geks().get(entID).getGek().get(tmpSearchTerm));
                                }
                            } else {
                                searchResults2graphEntityKeywords.put(entID, ft.getSearchResults2geks().get(entID));
                            }
                        }
                        //add entity accomplishment details to master list
                        for (Long entID2 : ft.getSearchResults2geas().keySet()) {
                            if (searchResults2graphEntityAccomps.containsKey(entID2)) {
                                for (Long tmpEntAccompId : ft.getSearchResults2geas().get(entID2).getGea().keySet()) {
                                    if (searchResults2graphEntityAccomps.get(entID2).getGea().containsKey(tmpEntAccompId)) {
                                    } else {
                                        searchResults2graphEntityAccomps.get(entID2).getGea().put(tmpEntAccompId, ft.getSearchResults2geas().get(entID2).getGea().get(tmpEntAccompId));
                                    }
                                }
                            } else {
                                searchResults2graphEntityAccomps.put(entID2, ft.getSearchResults2geas().get(entID2));
                            }
                        }
                        /*end search report*/

                        //TODO: remove if(esr1.size() < 30) once the scoring is implemented
                        //if (esr1.size() < 30) {
                        searchResultsWithKW.put(listOfEntityAccomplishments.get(eaid).getTitle().toLowerCase(), esr1);
                        searchResultsMain.addAll(esr1);
                        //} else {
                        searchResultsTMP.addAll(esr1);
                        //}

                        //this.QuerySet.getExpandQ1().add(new ExpandedKeyword(s));
                        //if(!blackList.contains(s.toLowerCase().trim())) {
                        //block of five was here


                    } else {
                        //check if in blacklist since keyword is short (3 words or less)
                        Boolean inBlacklist = false;  // used for blacklist check
                        //blacklist check
                        //System.out.println("entAccomp: "+listOfEntityAccomplishments.get(eaid).getTitle());
                        for (int blkX = 0; blkX < blacklistTerms.size(); blkX++) {
                            if (blacklistTerms.get(blkX).getBlacklistTerm().trim().toLowerCase().equals(listOfEntityAccomplishments.get(eaid).getTitle().trim().toLowerCase())) {
                                inBlacklist = true;
                            }
                        }


                        if (inBlacklist == false) {
                            shortDBKeyterms.add(listOfEntityAccomplishments.get(eaid).getTitle().trim()); //used for additional DISCO/Wordnet search
                            List<EntitySearchResults> esr2 = entitySearchResultsService.listSelectedEntityByIDResults(listOfEntityAccomplishments.get(eaid).getTitle().trim(), esr.getEntityID().toString());
                            //TODO: remove if(esr1.size() < 30) once the scoring is implemented
                            //if (esr2.size() < 30) {
                            searchResultsWithKW.put(listOfEntityAccomplishments.get(eaid).getTitle().toLowerCase(), esr2);
                            searchResultsMain.addAll(esr2);
                            //} else {
                            searchResultsTMP.addAll(esr2);

                            /*search report*/
                            //report tool
                            FormatTools ft = new FormatTools();
                            ft.entSearchRes2EntObjList2(esr2, listOfEntityAccomplishments.get(eaid).getTitle().trim(), null, distanceCalcSettings);
                            //add entity details to master list
                            for (Long entID : ft.getSearchResults2ge().keySet()) {
                                if (searchResults2graphEntity.containsKey(entID)) {
                                    searchResults2graphEntity.get(entID).setTotalWeight(searchResults2graphEntity.get(entID).getTotalWeight() + ft.getSearchResults2ge().get(entID).getTotalWeight());
//System.out.println("**** 2 : "+entID+" - weight is :"+searchResults2graphEntity.get(entID).getTotalWeight());
                                } else {
                                    searchResults2graphEntity.put(entID, ft.getSearchResults2ge().get(entID));
                                }
                            }
                            //add keyword details to master list
                            String tmpSearchTerm = listOfEntityAccomplishments.get(eaid).getTitle().trim();
                            for (Long entID : ft.getSearchResults2geks().keySet()) {
                                if (searchResults2graphEntityKeywords.containsKey(entID)) {
                                    //if keyword already exists
                                    if (searchResults2graphEntityKeywords.get(entID).getGek().containsKey(tmpSearchTerm)) {
                                        searchResults2graphEntityKeywords.get(entID).getGek().get(tmpSearchTerm).addAll(ft.getSearchResults2geks().get(entID).getGek().get(tmpSearchTerm));
                                    } else {
                                        searchResults2graphEntityKeywords.get(entID).getGek().put(tmpSearchTerm, ft.getSearchResults2geks().get(entID).getGek().get(tmpSearchTerm));
                                    }
                                } else {
                                    searchResults2graphEntityKeywords.put(entID, ft.getSearchResults2geks().get(entID));
                                }
                            }
                            //add entity accomplishment details to master list
                            for (Long entID2 : ft.getSearchResults2geas().keySet()) {
                                if (searchResults2graphEntityAccomps.containsKey(entID2)) {
                                    for (Long tmpEntAccompId : ft.getSearchResults2geas().get(entID2).getGea().keySet()) {
                                        if (searchResults2graphEntityAccomps.get(entID2).getGea().containsKey(tmpEntAccompId)) {
                                        } else {
                                            searchResults2graphEntityAccomps.get(entID2).getGea().put(tmpEntAccompId, ft.getSearchResults2geas().get(entID2).getGea().get(tmpEntAccompId));
                                        }
                                    }
                                } else {
                                    searchResults2graphEntityAccomps.put(entID2, ft.getSearchResults2geas().get(entID2));
                                }
                            }
                            /*end search report*/
                            //}

                        } else {
                            /////System.out.println("BlackListed:" + listOfEntityAccomplishments.get(eaid).getTitle());
                        }

                    }

                    /////System.out.println((eaid + 1) + "==" + listOfEntityAccomplishments.size());
                    /////System.out.println(blockOfFive);
                    //TODO Last block not done
                    if (!blockOfFive.equals("") && (countBlockItems == 5 || ((eaid + 1) == listOfEntityAccomplishments.size()))) {
                        try {
                            //if (s.split(" ").length > 2) {
                            /////System.out.println("Raw: " + blockOfFive);
                            urlcn.setFullText(blockOfFive);
                            urlcn.createURLWithText();
                            urlcn.startTermExtract();
                            urlcn.extractXMLKeywords();
                            // /////System.out.println("List of keywords "+mainCount+": "+urlcn.getExtractedKeywords().toString());
                            countBlockItems = 0;
                            blockOfFive = "";
                            //}
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    mainCount++;
                }
                //End Yahoo Term extractor
                //searchResultsMain.addAll(entitySearchResultsService.listSelectedEntitySearchResults(keywordVal));
            }
            /////System.out.println("Final List of generated keywords: " + urlcn.getExtractedKeywords().toString());
            /////System.out.println("Final List of keywords: ");
            /////System.out.println("END entityid: " + esr.getEntityID().toString());


            /*Problem with Yahoo keyword extract. If no keyworkds returned skip loop.*/
            //if(urlcn.getExtractedKeywords().size()>0) {
            for (int kwdnum = 0; kwdnum < urlcn.getExtractedKeywords().size(); kwdnum++) {

                if (urlcn.getExtractedKeywords().get(kwdnum).toString() != null) {
                    if (!searchResultsWithKW.containsKey(urlcn.getExtractedKeywords().get(kwdnum).toString().toLowerCase())) {
                        //TODO: check keyword against the black list
                        //TODO: remove duplicate entries (An entity could match more than one keyword
                        //      hence exist in more than one query result.
                        //searchResultsMain.addAll(entitySearchResultsService.listSelectedEntitySearchResults(urlcn.getExtractedKeywords().get(kwdnum).toString()));

                        Boolean inBlacklist = false;  // used for blacklist check
                        //blacklist check
                        for (int blkX = 0; blkX < blacklistTerms.size(); blkX++) {
                            if (blacklistTerms.get(blkX).getBlacklistTerm().trim().toLowerCase().equals(urlcn.getExtractedKeywords().get(kwdnum).toString().trim().toLowerCase())) {
                                inBlacklist = true;
                            }
                        }

                        if (inBlacklist == false) {
                            shortDBKeyterms.add(urlcn.getExtractedKeywords().get(kwdnum).toString()); //used for additional DISCO/Wordnet search
                            List<EntitySearchResults> esr3 = entitySearchResultsService.listSelectedEntityByIDResults(urlcn.getExtractedKeywords().get(kwdnum).toString(), esr.getEntityID().toString());
                            //TODO: remove if(esr1.size() < 30) once the scoring is implemented (showSearchAndResultsEntity)
                            //if (esr3.size() < 30) {
                            searchResultsWithKW.put(urlcn.getExtractedKeywords().get(kwdnum).toString().toLowerCase(), esr3);
                            searchResultsMain.addAll(esr3);
                            //} else {
                            searchResultsTMP.addAll(esr3);
                            /*search report*/
                            //report tool
                            FormatTools ft = new FormatTools();
                            ft.entSearchRes2EntObjList2(esr3, urlcn.getExtractedKeywords().get(kwdnum).toString().trim(), null, distanceCalcSettings);
                            //add entity details to master list
                            for (Long entID : ft.getSearchResults2ge().keySet()) {
                                if (searchResults2graphEntity.containsKey(entID)) {
                                    searchResults2graphEntity.get(entID).setTotalWeight(searchResults2graphEntity.get(entID).getTotalWeight() + ft.getSearchResults2ge().get(entID).getTotalWeight());
//System.out.println("**** 3: "+entID+" - weight is :"+searchResults2graphEntity.get(entID).getTotalWeight());
                                } else {
                                    searchResults2graphEntity.put(entID, ft.getSearchResults2ge().get(entID));
                                    
                                }
                            }
                            //add keyword details to master list
                            String tmpSearchTerm = urlcn.getExtractedKeywords().get(kwdnum).toString().trim();
                            for (Long entID : ft.getSearchResults2geks().keySet()) {
                                if (searchResults2graphEntityKeywords.containsKey(entID)) {
                                    //if keyword already exists
                                    if (searchResults2graphEntityKeywords.get(entID).getGek().containsKey(tmpSearchTerm)) {
                                        searchResults2graphEntityKeywords.get(entID).getGek().get(tmpSearchTerm).addAll(ft.getSearchResults2geks().get(entID).getGek().get(tmpSearchTerm));
                                    } else {
                                        searchResults2graphEntityKeywords.get(entID).getGek().put(tmpSearchTerm, ft.getSearchResults2geks().get(entID).getGek().get(tmpSearchTerm));
                                    }
                                } else {
                                    searchResults2graphEntityKeywords.put(entID, ft.getSearchResults2geks().get(entID));
                                }
                            }
                            //add entity accomplishment details to master list
                            for (Long entID2 : ft.getSearchResults2geas().keySet()) {
                                if (searchResults2graphEntityAccomps.containsKey(entID2)) {
                                    for (Long tmpEntAccompId : ft.getSearchResults2geas().get(entID2).getGea().keySet()) {
                                        if (searchResults2graphEntityAccomps.get(entID2).getGea().containsKey(tmpEntAccompId)) {
                                        } else {
                                            searchResults2graphEntityAccomps.get(entID2).getGea().put(tmpEntAccompId, ft.getSearchResults2geas().get(entID2).getGea().get(tmpEntAccompId));
                                        }
                                    }
                                } else {
                                    searchResults2graphEntityAccomps.put(entID2, ft.getSearchResults2geas().get(entID2));
                                }
                            }
                            /*end search report*/
                            //}
                        } else {
                            /////System.out.println("BlackListed:" + urlcn.getExtractedKeywords().get(kwdnum).toString());
                        }
                    } else {
                        System.out.println("It was nullllllllllllllllllllllllllll 6");
                    }

                }
            }
            //} //end if(urlcn.getExtractedKeywords().size()>0)
            //System.out.println("1: Short keywords are:" + shortDBKeyterms.toString());
            //TEST CODE: For DB keywords of less than two words get DISCOWordNet synonyms
            //Only use synonyms which contain keyword as a whole.
            //DiscoWordnetService discowordnet = new DiscoWordnetService();
            //discowordnet.setAccessKey("additional7185information1519regarding");

            //ConflationTool ct = new ConflationTool();
            UEALite uealite = new UEALite(); //stemming tool
            uealite.setMaxAcronymLength(30);
            uealite.setMaxAcronymLength(30);

            Inflector inf = new Inflector(); //prular / singular tool
            //ArrayList<String> searchStemmedPrularised = new ArrayList<String>(); //stores stemm and prular
            HashSet<String> searchStemmedPrularised = new HashSet<String>();

            for (int kwdnum2 = 0; kwdnum2 < shortDBKeyterms.size(); kwdnum2++) {
                searchStemmedPrularised.clear(); //initialise searchStemmedPrularised
                //System.out.println("In shortDBKeyterms:"+shortDBKeyterms.size()+" ::: "+kwdnum2);
                /*Problem with Yahoo keyword extract. If no keyworkds returned skip loop.*/
                if (shortDBKeyterms.get(kwdnum2).toString() != null) {

                    //if short keyword made of more than one word then only last word should be stemmed

                    String stemmedword = uealite.stem(shortDBKeyterms.get(kwdnum2).toString());
                    //add all permutations of the short keyword to the array ready to be searched for
                    searchStemmedPrularised.add(stemmedword);
                    searchStemmedPrularised.add(inf.pluralize(stemmedword));
                    searchStemmedPrularised.add(inf.pluralize(shortDBKeyterms.get(kwdnum2).toString().trim()));
                    searchStemmedPrularised.add(inf.singularize(shortDBKeyterms.get(kwdnum2).toString().trim()));

                    //System.out.println("stemmed/prularised/singularise words are: "+searchStemmedPrularised.toString());

                    for(String processedKeyword : searchStemmedPrularised) {
                        if(!processedKeyword.equals(shortDBKeyterms.get(kwdnum2).toString())) {

                    //blacklist check
                    Boolean inBlacklist = false;  // used for blacklist check
                    //blacklist check
                    for (int blkX = 0; blkX < blacklistTerms.size(); blkX++) {
                        if (blacklistTerms.get(blkX).getBlacklistTerm().trim().toLowerCase().equals(processedKeyword.trim().toLowerCase())) {
                            inBlacklist = true;
                        }
                    }

                    if (inBlacklist == false) {

                        if (!searchResultsWithKW.containsKey(processedKeyword.toLowerCase())) {

                            //System.out.println(dwKwordAsString + " NOT in the Black List");
                            List<EntitySearchResults> esr5 = entitySearchResultsService.listSelectedEntityByIDResults(processedKeyword.trim(), esr.getEntityID().toString());

                            //TODO: remove if(esr1.size() < 30) once the scoring is implemented (showSearchAndResultsEntity)
                            //if (esr3.size() < 30) {
                            searchResultsWithKW.put(processedKeyword.trim().toLowerCase(), esr5);
                            searchResultsMain.addAll(esr5);
                            //} else {
                            searchResultsTMP.addAll(esr5);
                            //search report//
                            //report tool

                            FormatTools ft = new FormatTools();
                            ft.entSearchRes2EntObjList2(esr5, processedKeyword.trim(), null, distanceCalcSettings);

                            //add entity details to master list
                            for (Long entID : ft.getSearchResults2ge().keySet()) {
                                if (searchResults2graphEntity.containsKey(entID)) {
                                  
                                    searchResults2graphEntity.get(entID).setTotalWeight(searchResults2graphEntity.get(entID).getTotalWeight() + ft.getSearchResults2ge().get(entID).getTotalWeight());
                                    //System.out.println("**** 3: "+entID+" - weight is :"+searchResults2graphEntity.get(entID).getTotalWeight());

                                } else {
                                    searchResults2graphEntity.put(entID, ft.getSearchResults2ge().get(entID));
                                    
                                    //System.out.println(entID + " weight is: "+searchResults2graphEntity.get(entID).getTotalWeight());
                                    //System.out.println(entID + " new entity should have been added: ");
                                }
                            }

                            //add keyword details to master list
                            String tmpSearchTerm = processedKeyword.trim();
                            for (Long entID : ft.getSearchResults2geks().keySet()) {
                                if (searchResults2graphEntityKeywords.containsKey(entID)) {
                                    //if keyword already exists
                                    if (searchResults2graphEntityKeywords.get(entID).getGek().containsKey(tmpSearchTerm)) {
                                       
                                        searchResults2graphEntityKeywords.get(entID).getGek().get(tmpSearchTerm).addAll(ft.getSearchResults2geks().get(entID).getGek().get(tmpSearchTerm));
                                    } else {
                                       
                                        searchResults2graphEntityKeywords.get(entID).getGek().put(tmpSearchTerm, ft.getSearchResults2geks().get(entID).getGek().get(tmpSearchTerm));
                                    }
                                } else {
                                   
                                    searchResults2graphEntityKeywords.put(entID, ft.getSearchResults2geks().get(entID));
                                }
                            }
                            //add entity accomplishment details to master list
                            for (Long entID2 : ft.getSearchResults2geas().keySet()) {
                                if (searchResults2graphEntityAccomps.containsKey(entID2)) {
                                    for (Long tmpEntAccompId : ft.getSearchResults2geas().get(entID2).getGea().keySet()) {
                                        if (searchResults2graphEntityAccomps.get(entID2).getGea().containsKey(tmpEntAccompId)) {
                                        } else {
                                            searchResults2graphEntityAccomps.get(entID2).getGea().put(tmpEntAccompId, ft.getSearchResults2geas().get(entID2).getGea().get(tmpEntAccompId));
                                        }
                                    }
                                } else {
                                    searchResults2graphEntityAccomps.put(entID2, ft.getSearchResults2geas().get(entID2));
                                }
                            }

                            //*end search report//

                        } else {
                            System.out.println("It was nullllllllllllllllllllllllllll 5");
                        }

                    }
                }//end processedKeyword
                    } //end if processedKeyword = shortDBKeyterms.get(kwdnum2).toString()
                    
                }
            }
            //END TEST CODE

            //TODO: remove all from if(searchResultsMain.isEmpty()) and if else  once the scoring is implemented (showSearchAndResultsEntity)
            if (searchResultsMain.isEmpty()) {
                if (!searchResultsTMP.isEmpty()) {
                    
                    searchResultsMain.addAll(searchResultsTMP);
                }
            } //else if (searchResultsMain.size() < 120) {
            //if ((searchResultsTMP.size() + searchResultsMain.size()) < 120) {
            else {
                searchResultsMain.addAll(searchResultsTMP);
            }
            //}
            //}

            /////System.out.println("1.KEYWORD SEARCH RESULT ::::::: " + searchResultsWithKW.size());
            //create visualDisplay (xml)

            pageToDisplay = "connectapp";
            formObject = new SearchForm();
            ReportTool repTool = new ReportTool();
            String reportCSV = repTool.createSearchReportAndDetailsXML(searchResults2graphEntity, searchResults2graphEntityAccomps, searchResults2graphEntityKeywords, validatedFormValues.get("searchTerm1"), centralNode);
            String additionalDataXML = repTool.getEntityAdditionalDataXML();
            System.out.println("additional xml"+additionalDataXML);

            //return new ModelAndView(pageToDisplay, "command", formObject).addObject("searchresults", searchResultsMain).addObject("visualDataSource", this.visualDataSource(searchResultsMain, validatedFormValues.get("searchTerm1")));
            //return new ModelAndView(pageToDisplay, "command", formObject).addObject("searchresults", searchResultsMain).addObject("visualDataSource", this.visualDataSourceEntityBased(searchResultsMain, validatedFormValues.get("searchTerm1"), this.createDisplayList(searchResultsWithKW), esr, searchResults2graphEntity)).addObject("entityAdditionalDataXML", additionalDataXML).addObject("reportDataSource", reportCSV).addObject("highestWeightValue", this.tmpHighestWeight);
            //.addObject("reportDataSource", repTool.createSearchReport(formatedSearchResults, validatedFormValues.get("searchTerm1")));

            return new ModelAndView(pageToDisplay, "command", formObject)
                    .addObject("searchresults", searchResultsMain)
                    .addObject("visualDataSource", this.visualDataSourceEntityBased(searchResultsMain, validatedFormValues.get("searchTerm1"), this.createDisplayList(searchResultsWithKW), esr, searchResults2graphEntity))
                    .addObject("entityAdditionalDataXML", additionalDataXML)
                    .addObject("reportDataSource", reportCSV)
                    .addObject("highestWeightValue", this.tmpHighestWeight);

        }

    }



    
    /*
     * This is for Entity (Person) search by entityID
     */
    //TODO: Entity (Person) search
    @RequestMapping(value = "/connectapp", method = RequestMethod.POST, params = "searchAnEntity")
    public ModelAndView showSearchAndResultsEntityID(SelectAnEntityForm searchForm, BindingResult result) {
        /////System.out.println("**** You have selected an Entity ID!!!!" + searchForm.getSearchAnEntity());

        SearchFormValidator sfv = new SearchFormValidator(); //input validation tool
        HashMap<String, String> validatedFormValues = new HashMap<String, String>(); //contains validated input values

        ArrayList<EntitySearchResults> searchResultsMain = new ArrayList<EntitySearchResults>();
        //TODO: remove searchResultsTMP once scoring implemented
        ArrayList<EntitySearchResults> searchResultsTMP = new ArrayList<EntitySearchResults>();
        //TODO: Key for searchResultsWithKW is a string(keyword) which can be very long. How long can the key be in chars
        HashMap<String, List<EntitySearchResults>> searchResultsWithKW = new HashMap<String, List<EntitySearchResults>>();
        List<Blacklist> blacklistTerms = blacklistService.listAllBlacklistTerms();
        //formated search results - entity keyed hashmap
        HashMap<Long, GraphEntity> searchResults2graphEntity = new HashMap<Long, GraphEntity>();
        //EntityID, GraphEntityKeywords
        HashMap<Long, GraphEntityKeywords> searchResults2graphEntityKeywords = new HashMap<Long, GraphEntityKeywords>();
        //EntityID, GraphEntityAccomps
        HashMap<Long, GraphEntityAccomps> searchResults2graphEntityAccomps = new HashMap<Long, GraphEntityAccomps>();

        List<DistanceCalcSettings> distanceCalcSettings = distanceCalcSettingsService.getAllDistanceCalcSettings();

        validatedFormValues.put("searchEntityID", sfv.validateSearchTerm(searchForm.getSearchAnEntity()));

        List searchResults = entitySearchResultsService.listEntityLookUpByID(validatedFormValues.get("searchEntityID"));

        String pageToDisplay = "";

        Object formObject = new SearchForm();

        //if one result only
            EntityLookUp esr = (EntityLookUp) searchResults.get(0);
            //set central node info for report and details XML
            HashMap<String, String> centralNode = new HashMap<String, String>();
            centralNode.put("name", esr.getEntityFname() + " " + esr.getEntitySname());
            centralNode.put("id", esr.getEntityID().toString());

            YahooTermExtractor urlcn = new YahooTermExtractor();
            //EntityAttributes entsAndAttribs = new EntityAttributes();

            //HashMap<String, GraphEntity> searchResults2graphEntity = new HashMap<String, GraphEntity>();
            //formated search results - entity keyed hashmap
            HashMap<String, GraphEntity> formatedSearchResults = new HashMap<String, GraphEntity>();
            String formatedSearchReport = ""; //report content

            //list of searched keywords, eliminate duplicates
            ArrayList<String> alreadySearchedKeyTerms = new ArrayList<String>();

            //get persons accomplishments
            List<EntityAccomplishment> listOfEntityAccomplishments = entityAccomplishmentService.listSelectedEntityAccomp(esr.getEntityID().toString());
            //genereate keywords

            /////System.out.println("START " + listOfEntityAccomplishments.size());
            //then search for matching entities
            //Yahoo term extractor
            int countBlockItems = 0;
            int mainCount = 1;
            String blockOfFive = "";

            ArrayList<String> shortDBKeyterms = new ArrayList<String>();
/*
            for (int eaid = 0; eaid < listOfEntityAccomplishments.size(); eaid++) {
                //if (!alreadySearchedKeyTerms.contains(listOfEntityAccomplishments.get(eaid).getTitle().trim())) {
                //if keyterm already searched for then ignore it to avoid duplicate results and incorrect match score
                if (!searchResultsWithKW.containsKey(listOfEntityAccomplishments.get(eaid).getTitle().toLowerCase())) {
                    //System.out.println(">88888< " + listOfEntityAccomplishments.get(eaid).getTitle());
                    HashMap<String, String> keywordVal = new HashMap<String, String>();
                    keywordVal.put("searchTerm1", listOfEntityAccomplishments.get(eaid).getTitle());

                    //if (!blackList.contains(s.toLowerCase().trim()) && !s.trim().equals("") && s.length()>0) {

                    if (listOfEntityAccomplishments.size() == mainCount) {
                        countBlockItems = 4;
                    }
                    //QuerySet.getExpandQ1().add(new ExpandedKeyword(s));

                    if (listOfEntityAccomplishments.get(eaid).getTitle().split(" ").length > 2) {

                        if (countBlockItems < 5 && !listOfEntityAccomplishments.get(eaid).getTitle().equals("")) {
                            //Use . to indicate end of sentance.  Yeahoo extractor works better.
                            blockOfFive = blockOfFive + ". " + listOfEntityAccomplishments.get(eaid).getTitle().replace('"', ' ');
                            countBlockItems++;
                        }

                        //Search keywords from database
                        //black list check not required since black list contains chort keywords
                        List<EntitySearchResults> esr1 = entitySearchResultsService.listSelectedEntityByIDResults(listOfEntityAccomplishments.get(eaid).getTitle().trim(), esr.getEntityID().toString());
                        //search report
                        //report tool
                        FormatTools ft = new FormatTools();
                        ft.entSearchRes2EntObjList2(esr1, listOfEntityAccomplishments.get(eaid).getTitle().trim(), null, distanceCalcSettings);
                        //add entity details to master list
                        for (Long entID : ft.getSearchResults2ge().keySet()) {
                            if (searchResults2graphEntity.containsKey(entID)) {
                                searchResults2graphEntity.get(entID).setTotalWeight(searchResults2graphEntity.get(entID).getTotalWeight() + ft.getSearchResults2ge().get(entID).getTotalWeight());
//System.out.println("**** 1: "+entID+" - weight is :"+searchResults2graphEntity.get(entID).getTotalWeight());
                            } else {
                                searchResults2graphEntity.put(entID, ft.getSearchResults2ge().get(entID));
                            }
                        }
                        //add keyword details to master list
                        String tmpSearchTerm = listOfEntityAccomplishments.get(eaid).getTitle().trim();
                        for (Long entID : ft.getSearchResults2geks().keySet()) {
                            if (searchResults2graphEntityKeywords.containsKey(entID)) {
                                //if keyword already exists
                                if (searchResults2graphEntityKeywords.get(entID).getGek().containsKey(tmpSearchTerm)) {
                                    searchResults2graphEntityKeywords.get(entID).getGek().get(tmpSearchTerm).addAll(ft.getSearchResults2geks().get(entID).getGek().get(tmpSearchTerm));
                                } else {
                                    searchResults2graphEntityKeywords.get(entID).getGek().put(tmpSearchTerm, ft.getSearchResults2geks().get(entID).getGek().get(tmpSearchTerm));
                                }
                            } else {
                                searchResults2graphEntityKeywords.put(entID, ft.getSearchResults2geks().get(entID));
                            }
                        }
                        //add entity accomplishment details to master list
                        for (Long entID2 : ft.getSearchResults2geas().keySet()) {
                            if (searchResults2graphEntityAccomps.containsKey(entID2)) {
                                for (Long tmpEntAccompId : ft.getSearchResults2geas().get(entID2).getGea().keySet()) {
                                    if (searchResults2graphEntityAccomps.get(entID2).getGea().containsKey(tmpEntAccompId)) {
                                    } else {
                                        searchResults2graphEntityAccomps.get(entID2).getGea().put(tmpEntAccompId, ft.getSearchResults2geas().get(entID2).getGea().get(tmpEntAccompId));
                                    }
                                }
                            } else {
                                searchResults2graphEntityAccomps.put(entID2, ft.getSearchResults2geas().get(entID2));
                            }
                        }
                        //end search report/

                        //TODO: remove if(esr1.size() < 30) once the scoring is implemented
                        //if (esr1.size() < 30) {
                        searchResultsWithKW.put(listOfEntityAccomplishments.get(eaid).getTitle().toLowerCase(), esr1);
                        searchResultsMain.addAll(esr1);
                        //} else {
                        searchResultsTMP.addAll(esr1);
                        //}

                        //this.QuerySet.getExpandQ1().add(new ExpandedKeyword(s));
                        //if(!blackList.contains(s.toLowerCase().trim())) {
                        //block of five was here


                    } else {
                        //check if in blacklist since keyword is short (3 words or less)
                        Boolean inBlacklist = false;  // used for blacklist check
                        //blacklist check
                        //System.out.println("entAccomp: "+listOfEntityAccomplishments.get(eaid).getTitle());
                        for (int blkX = 0; blkX < blacklistTerms.size(); blkX++) {
                            if (blacklistTerms.get(blkX).getBlacklistTerm().trim().toLowerCase().equals(listOfEntityAccomplishments.get(eaid).getTitle().trim().toLowerCase())) {
                                inBlacklist = true;
                            }
                        }


                        if (inBlacklist == false) {
                            shortDBKeyterms.add(listOfEntityAccomplishments.get(eaid).getTitle().trim()); //used for additional DISCO/Wordnet search
                            List<EntitySearchResults> esr2 = entitySearchResultsService.listSelectedEntityByIDResults(listOfEntityAccomplishments.get(eaid).getTitle().trim(), esr.getEntityID().toString());
                            //TODO: remove if(esr1.size() < 30) once the scoring is implemented
                            //if (esr2.size() < 30) {
                            searchResultsWithKW.put(listOfEntityAccomplishments.get(eaid).getTitle().toLowerCase(), esr2);
                            searchResultsMain.addAll(esr2);
                            //} else {
                            searchResultsTMP.addAll(esr2);

                            //search report
                            //report tool
                            FormatTools ft = new FormatTools();
                            ft.entSearchRes2EntObjList2(esr2, listOfEntityAccomplishments.get(eaid).getTitle().trim(), null, distanceCalcSettings);
                            //add entity details to master list
                            for (Long entID : ft.getSearchResults2ge().keySet()) {
                                if (searchResults2graphEntity.containsKey(entID)) {
                                    searchResults2graphEntity.get(entID).setTotalWeight(searchResults2graphEntity.get(entID).getTotalWeight() + ft.getSearchResults2ge().get(entID).getTotalWeight());
//System.out.println("**** 2 : "+entID+" - weight is :"+searchResults2graphEntity.get(entID).getTotalWeight());
                                } else {
                                    searchResults2graphEntity.put(entID, ft.getSearchResults2ge().get(entID));
                                }
                            }
                            //add keyword details to master list
                            String tmpSearchTerm = listOfEntityAccomplishments.get(eaid).getTitle().trim();
                            for (Long entID : ft.getSearchResults2geks().keySet()) {
                                if (searchResults2graphEntityKeywords.containsKey(entID)) {
                                    //if keyword already exists
                                    if (searchResults2graphEntityKeywords.get(entID).getGek().containsKey(tmpSearchTerm)) {
                                        searchResults2graphEntityKeywords.get(entID).getGek().get(tmpSearchTerm).addAll(ft.getSearchResults2geks().get(entID).getGek().get(tmpSearchTerm));
                                    } else {
                                        searchResults2graphEntityKeywords.get(entID).getGek().put(tmpSearchTerm, ft.getSearchResults2geks().get(entID).getGek().get(tmpSearchTerm));
                                    }
                                } else {
                                    searchResults2graphEntityKeywords.put(entID, ft.getSearchResults2geks().get(entID));
                                }
                            }
                            //add entity accomplishment details to master list
                            for (Long entID2 : ft.getSearchResults2geas().keySet()) {
                                if (searchResults2graphEntityAccomps.containsKey(entID2)) {
                                    for (Long tmpEntAccompId : ft.getSearchResults2geas().get(entID2).getGea().keySet()) {
                                        if (searchResults2graphEntityAccomps.get(entID2).getGea().containsKey(tmpEntAccompId)) {
                                        } else {
                                            searchResults2graphEntityAccomps.get(entID2).getGea().put(tmpEntAccompId, ft.getSearchResults2geas().get(entID2).getGea().get(tmpEntAccompId));
                                        }
                                    }
                                } else {
                                    searchResults2graphEntityAccomps.put(entID2, ft.getSearchResults2geas().get(entID2));
                                }
                            }
                            //end search report/
                            //}

                        } else {
                            /////System.out.println("BlackListed:" + listOfEntityAccomplishments.get(eaid).getTitle());
                        }

                    }

                    /////System.out.println((eaid + 1) + "==" + listOfEntityAccomplishments.size());
                    /////System.out.println(blockOfFive);
                    //TODO Last block not done
                    if (!blockOfFive.equals("") && (countBlockItems == 5 || ((eaid + 1) == listOfEntityAccomplishments.size()))) {
                        try {
                            //if (s.split(" ").length > 2) {
                            /////System.out.println("Raw: " + blockOfFive);
                            urlcn.setFullText(blockOfFive);
                            urlcn.createURLWithText();
                            urlcn.startTermExtract();
                            urlcn.extractXMLKeywords();
                            // /////System.out.println("List of keywords "+mainCount+": "+urlcn.getExtractedKeywords().toString());
                            countBlockItems = 0;
                            blockOfFive = "";
                            //}
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    mainCount++;
                }
                //End Yahoo Term extractor
                //searchResultsMain.addAll(entitySearchResultsService.listSelectedEntitySearchResults(keywordVal));
            }
            /////System.out.println("Final List of generated keywords: " + urlcn.getExtractedKeywords().toString());
            /////System.out.println("Final List of keywords: ");
            /////System.out.println("END entityid: " + esr.getEntityID().toString());


            //Problem with Yahoo keyword extract. If no keyworkds returned skip loop.
            //if(urlcn.getExtractedKeywords().size()>0) {
            for (int kwdnum = 0; kwdnum < urlcn.getExtractedKeywords().size(); kwdnum++) {

                if (urlcn.getExtractedKeywords().get(kwdnum).toString() != null) {
                    if (!searchResultsWithKW.containsKey(urlcn.getExtractedKeywords().get(kwdnum).toString().toLowerCase())) {
                        //TODO: check keyword against the black list
                        //TODO: remove duplicate entries (An entity could match more than one keyword
                        //      hence exist in more than one query result.
                        //searchResultsMain.addAll(entitySearchResultsService.listSelectedEntitySearchResults(urlcn.getExtractedKeywords().get(kwdnum).toString()));

                        Boolean inBlacklist = false;  // used for blacklist check
                        //blacklist check
                        for (int blkX = 0; blkX < blacklistTerms.size(); blkX++) {
                            if (blacklistTerms.get(blkX).getBlacklistTerm().trim().toLowerCase().equals(urlcn.getExtractedKeywords().get(kwdnum).toString().trim().toLowerCase())) {
                                inBlacklist = true;
                            }
                        }

                        if (inBlacklist == false) {
                            shortDBKeyterms.add(urlcn.getExtractedKeywords().get(kwdnum).toString()); //used for additional DISCO/Wordnet search
                            List<EntitySearchResults> esr3 = entitySearchResultsService.listSelectedEntityByIDResults(urlcn.getExtractedKeywords().get(kwdnum).toString(), esr.getEntityID().toString());
                            //TODO: remove if(esr1.size() < 30) once the scoring is implemented (showSearchAndResultsEntity)
                            //if (esr3.size() < 30) {
                            searchResultsWithKW.put(urlcn.getExtractedKeywords().get(kwdnum).toString().toLowerCase(), esr3);
                            searchResultsMain.addAll(esr3);
                            //} else {
                            searchResultsTMP.addAll(esr3);
                            //search report
                            //report tool
                            FormatTools ft = new FormatTools();
                            ft.entSearchRes2EntObjList2(esr3, urlcn.getExtractedKeywords().get(kwdnum).toString().trim(), null, distanceCalcSettings);
                            //add entity details to master list
                            for (Long entID : ft.getSearchResults2ge().keySet()) {
                                if (searchResults2graphEntity.containsKey(entID)) {
                                    searchResults2graphEntity.get(entID).setTotalWeight(searchResults2graphEntity.get(entID).getTotalWeight() + ft.getSearchResults2ge().get(entID).getTotalWeight());
//System.out.println("**** 3: "+entID+" - weight is :"+searchResults2graphEntity.get(entID).getTotalWeight());
                                } else {
                                    searchResults2graphEntity.put(entID, ft.getSearchResults2ge().get(entID));
                                }
                            }
                            //add keyword details to master list
                            String tmpSearchTerm = urlcn.getExtractedKeywords().get(kwdnum).toString().trim();
                            for (Long entID : ft.getSearchResults2geks().keySet()) {
                                if (searchResults2graphEntityKeywords.containsKey(entID)) {
                                    //if keyword already exists
                                    if (searchResults2graphEntityKeywords.get(entID).getGek().containsKey(tmpSearchTerm)) {
                                        searchResults2graphEntityKeywords.get(entID).getGek().get(tmpSearchTerm).addAll(ft.getSearchResults2geks().get(entID).getGek().get(tmpSearchTerm));
                                    } else {
                                        searchResults2graphEntityKeywords.get(entID).getGek().put(tmpSearchTerm, ft.getSearchResults2geks().get(entID).getGek().get(tmpSearchTerm));
                                    }
                                } else {
                                    searchResults2graphEntityKeywords.put(entID, ft.getSearchResults2geks().get(entID));
                                }
                            }
                            //add entity accomplishment details to master list
                            for (Long entID2 : ft.getSearchResults2geas().keySet()) {
                                if (searchResults2graphEntityAccomps.containsKey(entID2)) {
                                    for (Long tmpEntAccompId : ft.getSearchResults2geas().get(entID2).getGea().keySet()) {
                                        if (searchResults2graphEntityAccomps.get(entID2).getGea().containsKey(tmpEntAccompId)) {
                                        } else {
                                            searchResults2graphEntityAccomps.get(entID2).getGea().put(tmpEntAccompId, ft.getSearchResults2geas().get(entID2).getGea().get(tmpEntAccompId));
                                        }
                                    }
                                } else {
                                    searchResults2graphEntityAccomps.put(entID2, ft.getSearchResults2geas().get(entID2));
                                }
                            }
                            //end search report
                            //}
                        } else {
                            /////System.out.println("BlackListed:" + urlcn.getExtractedKeywords().get(kwdnum).toString());
                        }
                    } else {
                        System.out.println("It was nullllllllllllllllllllllllllll 6");
                    }

                }
            }
            //} //end if(urlcn.getExtractedKeywords().size()>0)
            //System.out.println("1: Short keywords are:" + shortDBKeyterms.toString());
            //TEST CODE: For DB keywords of less than two words get DISCOWordNet synonyms
            //Only use synonyms which contain keyword as a whole.
            DiscoWordnetService discowordnet = new DiscoWordnetService();
            discowordnet.setAccessKey("additional7185information1519regarding");

            ConflationTool ct = new ConflationTool();

            for (int kwdnum2 = 0; kwdnum2 < shortDBKeyterms.size(); kwdnum2++) {
                //System.out.println("In shortDBKeyterms:");
                //Problem with Yahoo keyword extract. If no keyworkds returned skip loop.
                if (shortDBKeyterms.get(kwdnum2).toString() != null) {
                    //if (shortDBKeyterms.get(kwdnum2).toString() != null && urlcn.getExtractedKeywords().size()>0) {

                    //DO DISCO Wordnet search
                    //check D/W results and only select the ones that contain search term
                    //(ie. st=waste, dw=wastes then select).
                    discowordnet.setSearchKeyword(shortDBKeyterms.get(kwdnum2).toString().trim());
                    discowordnet.createURLWithText();
                    discowordnet.startDiscoWordnetService();
                    discowordnet.extractXMLKeywords();
                    //System.out.println("1: discowordnet result xml:"+discowordnet.getFullXMLResponse());
                    //System.out.println("1: discowordnet results for keyword '" + shortDBKeyterms.get(kwdnum2).toString() + "'" + ": " + discowordnet.getExtractedKeywords().toString());
                    //System.out.println("there are dw keywords: "+discowordnet.getExtractedKeywords().size());
                    //todo: put end of this loop down low.
                    //for(Iterator<String> dwKword = discowordnet.getExtractedKeywords().iterator(); dwKword.hasNext(); ) {
                    for (int kwdnum3 = 0; kwdnum3 < discowordnet.getExtractedKeywords().size(); kwdnum3++) {

                        String dwKwordAsString = discowordnet.getExtractedKeywords().get(kwdnum3).toString();

                        //blacklist check
                        Boolean inBlacklist = false;  // used for blacklist check
                        //blacklist check
                        for (int blkX = 0; blkX < blacklistTerms.size(); blkX++) {
                            if (blacklistTerms.get(blkX).getBlacklistTerm().trim().toLowerCase().equals(dwKwordAsString.trim().toLowerCase())) {
                                inBlacklist = true;
                            }
                        }

                        if (inBlacklist == false) {

                            //System.out.println("source - keyword is:" + shortDBKeyterms.get(kwdnum2).toString());
                            //System.out.println("DW - target - keyword is:" + dwKwordAsString);
                            //Conflation. check if the source word and the target word (DW term) match 66% +
                            if (ct.howManyMatchingChars(shortDBKeyterms.get(kwdnum2).toString().trim(), dwKwordAsString)) {
                                //System.out.println("The match is: true");
                                //System.out.println(ct.howManyMatchingChars(dwKwordAsString.trim(), "targetFromDW"));

                                if (!searchResultsWithKW.containsKey(dwKwordAsString.toLowerCase())) {

                                    //System.out.println(dwKwordAsString + " NOT in the Black List");
                                    List<EntitySearchResults> esr5 = entitySearchResultsService.listSelectedEntityByIDResults(dwKwordAsString.trim(), esr.getEntityID().toString());

                                    //TODO: remove if(esr1.size() < 30) once the scoring is implemented (showSearchAndResultsEntity)
                                    //if (esr3.size() < 30) {
                                    searchResultsWithKW.put(dwKwordAsString.trim().toLowerCase(), esr5);
                                    searchResultsMain.addAll(esr5);
                                    //} else {
                                    searchResultsTMP.addAll(esr5);
                                    //search report
                                    //report tool

                                    FormatTools ft = new FormatTools();
                                    ft.entSearchRes2EntObjList2(esr5, dwKwordAsString.trim(), null, distanceCalcSettings);

                                    //add entity details to master list
                                    for (Long entID : ft.getSearchResults2ge().keySet()) {
                                        if (searchResults2graphEntity.containsKey(entID)) {
                                            searchResults2graphEntity.get(entID).setTotalWeight(searchResults2graphEntity.get(entID).getTotalWeight() + ft.getSearchResults2ge().get(entID).getTotalWeight());
                                            //System.out.println("**** 3: "+entID+" - weight is :"+searchResults2graphEntity.get(entID).getTotalWeight());
                                        } else {
                                            searchResults2graphEntity.put(entID, ft.getSearchResults2ge().get(entID));
                                        }
                                    }

                                    //add keyword details to master list
                                    String tmpSearchTerm = dwKwordAsString.trim();
                                    for (Long entID : ft.getSearchResults2geks().keySet()) {
                                        if (searchResults2graphEntityKeywords.containsKey(entID)) {
                                            //if keyword already exists
                                            if (searchResults2graphEntityKeywords.get(entID).getGek().containsKey(tmpSearchTerm)) {
                                                searchResults2graphEntityKeywords.get(entID).getGek().get(tmpSearchTerm).addAll(ft.getSearchResults2geks().get(entID).getGek().get(tmpSearchTerm));
                                            } else {
                                                searchResults2graphEntityKeywords.get(entID).getGek().put(tmpSearchTerm, ft.getSearchResults2geks().get(entID).getGek().get(tmpSearchTerm));
                                            }
                                        } else {
                                            searchResults2graphEntityKeywords.put(entID, ft.getSearchResults2geks().get(entID));
                                        }
                                    }

                                    //add entity accomplishment details to master list
                                    for (Long entID2 : ft.getSearchResults2geas().keySet()) {
                                        if (searchResults2graphEntityAccomps.containsKey(entID2)) {
                                            for (Long tmpEntAccompId : ft.getSearchResults2geas().get(entID2).getGea().keySet()) {
                                                if (searchResults2graphEntityAccomps.get(entID2).getGea().containsKey(tmpEntAccompId)) {
                                                } else {
                                                    searchResults2graphEntityAccomps.get(entID2).getGea().put(tmpEntAccompId, ft.getSearchResults2geas().get(entID2).getGea().get(tmpEntAccompId));
                                                }
                                            }
                                        } else {
                                            searchResults2graphEntityAccomps.put(entID2, ft.getSearchResults2geas().get(entID2));
                                        }
                                    }

                                    //end search report/


                                } else {
                                    System.out.println("It was nullllllllllllllllllllllllllll 5");
                                }

                            } //end if
                            else {
                                //System.out.println("The match is: false");
                            }
                        } //end blacklist if/ else {
                            //System.out.println(dwKwordAsString + " in the Black List");
                        }
                    }// end for
                }
            }
            //END TEST CODE

            //TODO: remove all from if(searchResultsMain.isEmpty()) and if else  once the scoring is implemented (showSearchAndResultsEntity)
            if (searchResultsMain.isEmpty()) {
                if (!searchResultsTMP.isEmpty()) {
                    searchResultsMain.addAll(searchResultsTMP);
                }
            } //else if (searchResultsMain.size() < 120) {
            //if ((searchResultsTMP.size() + searchResultsMain.size()) < 120) {
            else {
                searchResultsMain.addAll(searchResultsTMP);
            }
            */
            //}
            //}

            for (int eaid = 0; eaid < listOfEntityAccomplishments.size(); eaid++) {
                //if (!alreadySearchedKeyTerms.contains(listOfEntityAccomplishments.get(eaid).getTitle().trim())) {
                //if keyterm already searched for then ignore it to avoid duplicate results and incorrect match score
                if (!searchResultsWithKW.containsKey(listOfEntityAccomplishments.get(eaid).getTitle().toLowerCase())) {
                    //System.out.println(">88888< " + listOfEntityAccomplishments.get(eaid).getTitle());
                    HashMap<String, String> keywordVal = new HashMap<String, String>();
                    keywordVal.put("searchTerm1", listOfEntityAccomplishments.get(eaid).getTitle());

                    //if (!blackList.contains(s.toLowerCase().trim()) && !s.trim().equals("") && s.length()>0) {

                    if (listOfEntityAccomplishments.size() == mainCount) {
                        countBlockItems = 4;
                    }
                    //QuerySet.getExpandQ1().add(new ExpandedKeyword(s));

                    if (listOfEntityAccomplishments.get(eaid).getTitle().split(" ").length > 2) {

                        if (countBlockItems < 5 && !listOfEntityAccomplishments.get(eaid).getTitle().equals("")) {
                            /*Use . to indicate end of sentance.  Yeahoo extractor works better.*/
                            blockOfFive = blockOfFive + ". " + listOfEntityAccomplishments.get(eaid).getTitle().replace('"', ' ');
                            countBlockItems++;
                        }

                        //Search keywords from database
                        //black list check not required since black list contains chort keywords
                        List<EntitySearchResults> esr1 = entitySearchResultsService.listSelectedEntityByIDResults(listOfEntityAccomplishments.get(eaid).getTitle().trim(), esr.getEntityID().toString());
                        /*search report*/
                        //report tool
                        FormatTools ft = new FormatTools();
                        ft.entSearchRes2EntObjList2(esr1, listOfEntityAccomplishments.get(eaid).getTitle().trim(), null, distanceCalcSettings);
                        //add entity details to master list
                        for (Long entID : ft.getSearchResults2ge().keySet()) {
                            if (searchResults2graphEntity.containsKey(entID)) {
                                searchResults2graphEntity.get(entID).setTotalWeight(searchResults2graphEntity.get(entID).getTotalWeight() + ft.getSearchResults2ge().get(entID).getTotalWeight());
//System.out.println("**** 1: "+entID+" - weight is :"+searchResults2graphEntity.get(entID).getTotalWeight());
                            } else {
                                searchResults2graphEntity.put(entID, ft.getSearchResults2ge().get(entID));
                            }
                        }
                        //add keyword details to master list
                        String tmpSearchTerm = listOfEntityAccomplishments.get(eaid).getTitle().trim();
                        for (Long entID : ft.getSearchResults2geks().keySet()) {
                            if (searchResults2graphEntityKeywords.containsKey(entID)) {
                                //if keyword already exists
                                if (searchResults2graphEntityKeywords.get(entID).getGek().containsKey(tmpSearchTerm)) {
                                    searchResults2graphEntityKeywords.get(entID).getGek().get(tmpSearchTerm).addAll(ft.getSearchResults2geks().get(entID).getGek().get(tmpSearchTerm));
                                } else {
                                    searchResults2graphEntityKeywords.get(entID).getGek().put(tmpSearchTerm, ft.getSearchResults2geks().get(entID).getGek().get(tmpSearchTerm));
                                }
                            } else {
                                searchResults2graphEntityKeywords.put(entID, ft.getSearchResults2geks().get(entID));
                            }
                        }
                        //add entity accomplishment details to master list
                        for (Long entID2 : ft.getSearchResults2geas().keySet()) {
                            if (searchResults2graphEntityAccomps.containsKey(entID2)) {
                                for (Long tmpEntAccompId : ft.getSearchResults2geas().get(entID2).getGea().keySet()) {
                                    if (searchResults2graphEntityAccomps.get(entID2).getGea().containsKey(tmpEntAccompId)) {
                                    } else {
                                        searchResults2graphEntityAccomps.get(entID2).getGea().put(tmpEntAccompId, ft.getSearchResults2geas().get(entID2).getGea().get(tmpEntAccompId));
                                    }
                                }
                            } else {
                                searchResults2graphEntityAccomps.put(entID2, ft.getSearchResults2geas().get(entID2));
                            }
                        }
                        /*end search report*/

                        //TODO: remove if(esr1.size() < 30) once the scoring is implemented
                        //if (esr1.size() < 30) {
                        searchResultsWithKW.put(listOfEntityAccomplishments.get(eaid).getTitle().toLowerCase(), esr1);
                        searchResultsMain.addAll(esr1);
                        //} else {
                        searchResultsTMP.addAll(esr1);
                        //}

                        //this.QuerySet.getExpandQ1().add(new ExpandedKeyword(s));
                        //if(!blackList.contains(s.toLowerCase().trim())) {
                        //block of five was here


                    } else {
                        //check if in blacklist since keyword is short (3 words or less)
                        Boolean inBlacklist = false;  // used for blacklist check
                        //blacklist check
                        //System.out.println("entAccomp: "+listOfEntityAccomplishments.get(eaid).getTitle());
                        for (int blkX = 0; blkX < blacklistTerms.size(); blkX++) {
                            if (blacklistTerms.get(blkX).getBlacklistTerm().trim().toLowerCase().equals(listOfEntityAccomplishments.get(eaid).getTitle().trim().toLowerCase())) {
                                inBlacklist = true;
                            }
                        }


                        if (inBlacklist == false) {
                            shortDBKeyterms.add(listOfEntityAccomplishments.get(eaid).getTitle().trim()); //used for additional DISCO/Wordnet search
                            List<EntitySearchResults> esr2 = entitySearchResultsService.listSelectedEntityByIDResults(listOfEntityAccomplishments.get(eaid).getTitle().trim(), esr.getEntityID().toString());
                            //TODO: remove if(esr1.size() < 30) once the scoring is implemented
                            //if (esr2.size() < 30) {
                            searchResultsWithKW.put(listOfEntityAccomplishments.get(eaid).getTitle().toLowerCase(), esr2);
                            searchResultsMain.addAll(esr2);
                            //} else {
                            searchResultsTMP.addAll(esr2);

                            /*search report*/
                            //report tool
                            FormatTools ft = new FormatTools();
                            ft.entSearchRes2EntObjList2(esr2, listOfEntityAccomplishments.get(eaid).getTitle().trim(), null, distanceCalcSettings);
                            //add entity details to master list
                            for (Long entID : ft.getSearchResults2ge().keySet()) {
                                if (searchResults2graphEntity.containsKey(entID)) {
                                    searchResults2graphEntity.get(entID).setTotalWeight(searchResults2graphEntity.get(entID).getTotalWeight() + ft.getSearchResults2ge().get(entID).getTotalWeight());
//System.out.println("**** 2 : "+entID+" - weight is :"+searchResults2graphEntity.get(entID).getTotalWeight());
                                } else {
                                    searchResults2graphEntity.put(entID, ft.getSearchResults2ge().get(entID));
                                }
                            }
                            //add keyword details to master list
                            String tmpSearchTerm = listOfEntityAccomplishments.get(eaid).getTitle().trim();
                            for (Long entID : ft.getSearchResults2geks().keySet()) {
                                if (searchResults2graphEntityKeywords.containsKey(entID)) {
                                    //if keyword already exists
                                    if (searchResults2graphEntityKeywords.get(entID).getGek().containsKey(tmpSearchTerm)) {
                                        searchResults2graphEntityKeywords.get(entID).getGek().get(tmpSearchTerm).addAll(ft.getSearchResults2geks().get(entID).getGek().get(tmpSearchTerm));
                                    } else {
                                        searchResults2graphEntityKeywords.get(entID).getGek().put(tmpSearchTerm, ft.getSearchResults2geks().get(entID).getGek().get(tmpSearchTerm));
                                    }
                                } else {
                                    searchResults2graphEntityKeywords.put(entID, ft.getSearchResults2geks().get(entID));
                                }
                            }
                            //add entity accomplishment details to master list
                            for (Long entID2 : ft.getSearchResults2geas().keySet()) {
                                if (searchResults2graphEntityAccomps.containsKey(entID2)) {
                                    for (Long tmpEntAccompId : ft.getSearchResults2geas().get(entID2).getGea().keySet()) {
                                        if (searchResults2graphEntityAccomps.get(entID2).getGea().containsKey(tmpEntAccompId)) {
                                        } else {
                                            searchResults2graphEntityAccomps.get(entID2).getGea().put(tmpEntAccompId, ft.getSearchResults2geas().get(entID2).getGea().get(tmpEntAccompId));
                                        }
                                    }
                                } else {
                                    searchResults2graphEntityAccomps.put(entID2, ft.getSearchResults2geas().get(entID2));
                                }
                            }
                            /*end search report*/
                            //}

                        } else {
                            /////System.out.println("BlackListed:" + listOfEntityAccomplishments.get(eaid).getTitle());
                        }

                    }

                    /////System.out.println((eaid + 1) + "==" + listOfEntityAccomplishments.size());
                    /////System.out.println(blockOfFive);
                    //TODO Last block not done
                    if (!blockOfFive.equals("") && (countBlockItems == 5 || ((eaid + 1) == listOfEntityAccomplishments.size()))) {
                        try {
                            //if (s.split(" ").length > 2) {
                            /////System.out.println("Raw: " + blockOfFive);
                            urlcn.setFullText(blockOfFive);
                            urlcn.createURLWithText();
                            urlcn.startTermExtract();
                            urlcn.extractXMLKeywords();
                            // /////System.out.println("List of keywords "+mainCount+": "+urlcn.getExtractedKeywords().toString());
                            countBlockItems = 0;
                            blockOfFive = "";
                            //}
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    mainCount++;
                }
                //End Yahoo Term extractor
                //searchResultsMain.addAll(entitySearchResultsService.listSelectedEntitySearchResults(keywordVal));
            }
            /////System.out.println("Final List of generated keywords: " + urlcn.getExtractedKeywords().toString());
            /////System.out.println("Final List of keywords: ");
            /////System.out.println("END entityid: " + esr.getEntityID().toString());


            /*Problem with Yahoo keyword extract. If no keyworkds returned skip loop.*/
            //if(urlcn.getExtractedKeywords().size()>0) {
            for (int kwdnum = 0; kwdnum < urlcn.getExtractedKeywords().size(); kwdnum++) {

                if (urlcn.getExtractedKeywords().get(kwdnum).toString() != null) {
                    if (!searchResultsWithKW.containsKey(urlcn.getExtractedKeywords().get(kwdnum).toString().toLowerCase())) {
                        //TODO: check keyword against the black list
                        //TODO: remove duplicate entries (An entity could match more than one keyword
                        //      hence exist in more than one query result.
                        //searchResultsMain.addAll(entitySearchResultsService.listSelectedEntitySearchResults(urlcn.getExtractedKeywords().get(kwdnum).toString()));

                        Boolean inBlacklist = false;  // used for blacklist check
                        //blacklist check
                        for (int blkX = 0; blkX < blacklistTerms.size(); blkX++) {
                            if (blacklistTerms.get(blkX).getBlacklistTerm().trim().toLowerCase().equals(urlcn.getExtractedKeywords().get(kwdnum).toString().trim().toLowerCase())) {
                                inBlacklist = true;
                            }
                        }

                        if (inBlacklist == false) {
                            shortDBKeyterms.add(urlcn.getExtractedKeywords().get(kwdnum).toString()); //used for additional DISCO/Wordnet search
                            List<EntitySearchResults> esr3 = entitySearchResultsService.listSelectedEntityByIDResults(urlcn.getExtractedKeywords().get(kwdnum).toString(), esr.getEntityID().toString());
                            //TODO: remove if(esr1.size() < 30) once the scoring is implemented (showSearchAndResultsEntity)
                            //if (esr3.size() < 30) {
                            searchResultsWithKW.put(urlcn.getExtractedKeywords().get(kwdnum).toString().toLowerCase(), esr3);
                            searchResultsMain.addAll(esr3);
                            //} else {
                            searchResultsTMP.addAll(esr3);
                            /*search report*/
                            //report tool
                            FormatTools ft = new FormatTools();
                            ft.entSearchRes2EntObjList2(esr3, urlcn.getExtractedKeywords().get(kwdnum).toString().trim(), null, distanceCalcSettings);
                            //add entity details to master list
                            for (Long entID : ft.getSearchResults2ge().keySet()) {
                                if (searchResults2graphEntity.containsKey(entID)) {
                                    searchResults2graphEntity.get(entID).setTotalWeight(searchResults2graphEntity.get(entID).getTotalWeight() + ft.getSearchResults2ge().get(entID).getTotalWeight());
//System.out.println("**** 3: "+entID+" - weight is :"+searchResults2graphEntity.get(entID).getTotalWeight());
                                } else {
                                    searchResults2graphEntity.put(entID, ft.getSearchResults2ge().get(entID));

                                }
                            }
                            //add keyword details to master list
                            String tmpSearchTerm = urlcn.getExtractedKeywords().get(kwdnum).toString().trim();
                            for (Long entID : ft.getSearchResults2geks().keySet()) {
                                if (searchResults2graphEntityKeywords.containsKey(entID)) {
                                    //if keyword already exists
                                    if (searchResults2graphEntityKeywords.get(entID).getGek().containsKey(tmpSearchTerm)) {
                                        searchResults2graphEntityKeywords.get(entID).getGek().get(tmpSearchTerm).addAll(ft.getSearchResults2geks().get(entID).getGek().get(tmpSearchTerm));
                                    } else {
                                        searchResults2graphEntityKeywords.get(entID).getGek().put(tmpSearchTerm, ft.getSearchResults2geks().get(entID).getGek().get(tmpSearchTerm));
                                    }
                                } else {
                                    searchResults2graphEntityKeywords.put(entID, ft.getSearchResults2geks().get(entID));
                                }
                            }
                            //add entity accomplishment details to master list
                            for (Long entID2 : ft.getSearchResults2geas().keySet()) {
                                if (searchResults2graphEntityAccomps.containsKey(entID2)) {
                                    for (Long tmpEntAccompId : ft.getSearchResults2geas().get(entID2).getGea().keySet()) {
                                        if (searchResults2graphEntityAccomps.get(entID2).getGea().containsKey(tmpEntAccompId)) {
                                        } else {
                                            searchResults2graphEntityAccomps.get(entID2).getGea().put(tmpEntAccompId, ft.getSearchResults2geas().get(entID2).getGea().get(tmpEntAccompId));
                                        }
                                    }
                                } else {
                                    searchResults2graphEntityAccomps.put(entID2, ft.getSearchResults2geas().get(entID2));
                                }
                            }
                            /*end search report*/
                            //}
                        } else {
                            /////System.out.println("BlackListed:" + urlcn.getExtractedKeywords().get(kwdnum).toString());
                        }
                    } else {
                        System.out.println("It was nullllllllllllllllllllllllllll 6");
                    }

                }
            }
            //} //end if(urlcn.getExtractedKeywords().size()>0)
            //System.out.println("1: Short keywords are:" + shortDBKeyterms.toString());
            //TEST CODE: For DB keywords of less than two words get DISCOWordNet synonyms
            //Only use synonyms which contain keyword as a whole.
            //DiscoWordnetService discowordnet = new DiscoWordnetService();
            //discowordnet.setAccessKey("additional7185information1519regarding");

            //ConflationTool ct = new ConflationTool();
            UEALite uealite = new UEALite(); //stemming tool
            uealite.setMaxAcronymLength(30);
            uealite.setMaxAcronymLength(30);

            Inflector inf = new Inflector(); //prular / singular tool
            //ArrayList<String> searchStemmedPrularised = new ArrayList<String>(); //stores stemm and prular
            HashSet<String> searchStemmedPrularised = new HashSet<String>();

            for (int kwdnum2 = 0; kwdnum2 < shortDBKeyterms.size(); kwdnum2++) {
                searchStemmedPrularised.clear(); //initialise searchStemmedPrularised
                //System.out.println("In shortDBKeyterms:"+shortDBKeyterms.size()+" ::: "+kwdnum2);
                /*Problem with Yahoo keyword extract. If no keyworkds returned skip loop.*/
                if (shortDBKeyterms.get(kwdnum2).toString() != null) {

                    //if short keyword made of more than one word then only last word should be stemmed

                    String stemmedword = uealite.stem(shortDBKeyterms.get(kwdnum2).toString());
                    //add all permutations of the short keyword to the array ready to be searched for
                    searchStemmedPrularised.add(stemmedword);
                    searchStemmedPrularised.add(inf.pluralize(stemmedword));
                    searchStemmedPrularised.add(inf.pluralize(shortDBKeyterms.get(kwdnum2).toString().trim()));
                    searchStemmedPrularised.add(inf.singularize(shortDBKeyterms.get(kwdnum2).toString().trim()));

                    //System.out.println("stemmed/prularised/singularise words are: "+searchStemmedPrularised.toString());

                    for(String processedKeyword : searchStemmedPrularised) {
                        if(!processedKeyword.equals(shortDBKeyterms.get(kwdnum2).toString())) {

                    //blacklist check
                    Boolean inBlacklist = false;  // used for blacklist check
                    //blacklist check
                    for (int blkX = 0; blkX < blacklistTerms.size(); blkX++) {
                        if (blacklistTerms.get(blkX).getBlacklistTerm().trim().toLowerCase().equals(processedKeyword.trim().toLowerCase())) {
                            inBlacklist = true;
                        }
                    }

                    if (inBlacklist == false) {

                        if (!searchResultsWithKW.containsKey(processedKeyword.toLowerCase())) {

                            //System.out.println(dwKwordAsString + " NOT in the Black List");
                            List<EntitySearchResults> esr5 = entitySearchResultsService.listSelectedEntityByIDResults(processedKeyword.trim(), esr.getEntityID().toString());

                            //TODO: remove if(esr1.size() < 30) once the scoring is implemented (showSearchAndResultsEntity)
                            //if (esr3.size() < 30) {
                            searchResultsWithKW.put(processedKeyword.trim().toLowerCase(), esr5);
                            searchResultsMain.addAll(esr5);
                            //} else {
                            searchResultsTMP.addAll(esr5);
                            //search report//
                            //report tool

                            FormatTools ft = new FormatTools();
                            ft.entSearchRes2EntObjList2(esr5, processedKeyword.trim(), null, distanceCalcSettings);

                            //add entity details to master list
                            for (Long entID : ft.getSearchResults2ge().keySet()) {
                                if (searchResults2graphEntity.containsKey(entID)) {

                                    searchResults2graphEntity.get(entID).setTotalWeight(searchResults2graphEntity.get(entID).getTotalWeight() + ft.getSearchResults2ge().get(entID).getTotalWeight());
                                    //System.out.println("**** 3: "+entID+" - weight is :"+searchResults2graphEntity.get(entID).getTotalWeight());

                                } else {
                                    searchResults2graphEntity.put(entID, ft.getSearchResults2ge().get(entID));

                                    //System.out.println(entID + " weight is: "+searchResults2graphEntity.get(entID).getTotalWeight());
                                    //System.out.println(entID + " new entity should have been added: ");
                                }
                            }

                            //add keyword details to master list
                            String tmpSearchTerm = processedKeyword.trim();
                            for (Long entID : ft.getSearchResults2geks().keySet()) {
                                if (searchResults2graphEntityKeywords.containsKey(entID)) {
                                    //if keyword already exists
                                    if (searchResults2graphEntityKeywords.get(entID).getGek().containsKey(tmpSearchTerm)) {

                                        searchResults2graphEntityKeywords.get(entID).getGek().get(tmpSearchTerm).addAll(ft.getSearchResults2geks().get(entID).getGek().get(tmpSearchTerm));
                                    } else {

                                        searchResults2graphEntityKeywords.get(entID).getGek().put(tmpSearchTerm, ft.getSearchResults2geks().get(entID).getGek().get(tmpSearchTerm));
                                    }
                                } else {

                                    searchResults2graphEntityKeywords.put(entID, ft.getSearchResults2geks().get(entID));
                                }
                            }
                            //add entity accomplishment details to master list
                            for (Long entID2 : ft.getSearchResults2geas().keySet()) {
                                if (searchResults2graphEntityAccomps.containsKey(entID2)) {
                                    for (Long tmpEntAccompId : ft.getSearchResults2geas().get(entID2).getGea().keySet()) {
                                        if (searchResults2graphEntityAccomps.get(entID2).getGea().containsKey(tmpEntAccompId)) {
                                        } else {
                                            searchResults2graphEntityAccomps.get(entID2).getGea().put(tmpEntAccompId, ft.getSearchResults2geas().get(entID2).getGea().get(tmpEntAccompId));
                                        }
                                    }
                                } else {
                                    searchResults2graphEntityAccomps.put(entID2, ft.getSearchResults2geas().get(entID2));
                                }
                            }

                            //*end search report//

                        } else {
                            System.out.println("It was nullllllllllllllllllllllllllll 5");
                        }

                    }
                }//end processedKeyword
                    } //end if processedKeyword = shortDBKeyterms.get(kwdnum2).toString()

                }
            }
            //END TEST CODE

            //TODO: remove all from if(searchResultsMain.isEmpty()) and if else  once the scoring is implemented (showSearchAndResultsEntity)
            if (searchResultsMain.isEmpty()) {
                if (!searchResultsTMP.isEmpty()) {

                    searchResultsMain.addAll(searchResultsTMP);
                }
            } //else if (searchResultsMain.size() < 120) {
            //if ((searchResultsTMP.size() + searchResultsMain.size()) < 120) {
            else {
                searchResultsMain.addAll(searchResultsTMP);
            }

            /////System.out.println("1.KEYWORD SEARCH RESULT ::::::: " + searchResultsWithKW.size());
            //create visualDisplay (xml)
            pageToDisplay = "connectapp";
            formObject = new SearchForm();
            ReportTool repTool = new ReportTool();
            String reportCSV = repTool.createSearchReportAndDetailsXML(searchResults2graphEntity, searchResults2graphEntityAccomps, searchResults2graphEntityKeywords, validatedFormValues.get("searchTerm1"), centralNode);
            String additionalDataXML = repTool.getEntityAdditionalDataXML();

            //return new ModelAndView(pageToDisplay, "command", formObject).addObject("searchresults", searchResultsMain).addObject("visualDataSource", this.visualDataSource(searchResultsMain, validatedFormValues.get("searchTerm1")));
            //return new ModelAndView(pageToDisplay, "command", formObject).addObject("searchresults", searchResultsMain).addObject("visualDataSource", this.visualDataSourceEntityBased(searchResultsMain, validatedFormValues.get("searchTerm1"), this.createDisplayList(searchResultsWithKW), esr, searchResults2graphEntity)).addObject("entityAdditionalDataXML", additionalDataXML).addObject("reportDataSource", reportCSV).addObject("highestWeightValue", this.tmpHighestWeight);
            //.addObject("reportDataSource", repTool.createSearchReport(formatedSearchResults, validatedFormValues.get("searchTerm1")));
            return new ModelAndView(pageToDisplay, "command", formObject).addObject("searchresults", searchResultsMain).addObject("visualDataSource", this.visualDataSourceEntityBased(searchResultsMain, validatedFormValues.get("searchTerm1"), this.createDisplayList(searchResultsWithKW), esr, searchResults2graphEntity)).addObject("entityAdditionalDataXML", additionalDataXML).addObject("reportDataSource", reportCSV).addObject("highestWeightValue", this.tmpHighestWeight);


        
    }

    @RequestMapping(value = "/wordnetdiscosyn", method = RequestMethod.GET, params = "searchterms")
    public ModelAndView wordnetDiscoSynonyms(@RequestParam("searchterms") String searchterms) {

        //System.out.println("First: "+ searchterms);

        try {
            DiscoWordnetService discowordnet = new DiscoWordnetService(searchterms, "additional7185information1519regarding");
            discowordnet.startDiscoWordnetService();
            String tmpvar = discowordnet.getFullXMLResponse();

            return new ModelAndView("wordnetdiscosyn").addObject("discowordnet", tmpvar);
        } catch (Exception e) {
            return new ModelAndView("wordnetdiscosyn").addObject("errorMsg", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Synonyms><error>search failed: </error></Synonyms>");
        }
    }

    @RequestMapping(value = "/wordnetdiscosyn", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView wordnetDiscoSynonyms() {
        String errorMsg = "Incorrect url path.  The correct syntax is: wordnetdiscosyn.html?searchterms=water,air,earth";
        return new ModelAndView("wordnetdiscosyn").addObject("errorMsg", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Synonyms><error>" + errorMsg + "</error></Synonyms>");
    }


    /*Test*/
    @ModelAttribute("searchOperatorValues")
    public List<String> getSearchOperatorValues() {
        List<String> soValues = new ArrayList<String>();
        soValues.add("None");
        soValues.add("AND");
        soValues.add("NOT");
        return soValues;
    }

    @ModelAttribute("searchTypeRadioBValues")
    public List<String> getSearchTypeRadioBValues() {
        List<String> stValues = new ArrayList<String>();
        stValues.add("Term (Default option)");
        stValues.add("Person-Entity");
        return stValues;
    }

    //TODO: Move this function to a seperate class
    private HashMap<String, HashMap<String, String>> createDisplayList(HashMap<String, List<EntitySearchResults>> searchResultsWithKW) {
        HashMap<String, HashMap<String, String>> displayList = new HashMap<String, HashMap<String, String>>();


        for (String key : searchResultsWithKW.keySet()) {
            ///////System.out.println("££££ In loop 1: ");
            List<EntitySearchResults> esr1 = (List) searchResultsWithKW.get(key);

            for (int x = 0; esr1.size() > x; x++) {
                ///////System.out.println("££££ In loop 2: ");
                if (displayList.containsKey(esr1.get(x).getEntityID().toString())) {
                    ///////System.out.println("££££ Adding In loop 2: ");
                    displayList.get(esr1.get(x).getEntityID().toString()).put(esr1.get(x).getAccompTitle(), key);
                } else {
                    HashMap<String, String> tmpHM1 = new HashMap<String, String>();
                    tmpHM1.put(esr1.get(x).getAccompTitle(), key);
                    displayList.put(esr1.get(x).getEntityID().toString(), tmpHM1);
                }

            }
        }
        return displayList;
        ///////System.out.println("££££New List has: "+displayList.size()+" and Num of keywords was: "+searchResultsWithKW.size());
    }

    /*
     * Validate and format input string
     */
    private String inputStringVandF(String inputStringValue) {
        //TODO: Move the input formating and validation to the controller.
        return "";
    }

    //TODO: move this to a class
    public String visualDataSourceEntityBased(List searchResults, String searchString, HashMap<String, HashMap<String, String>> displayDetailsKW, EntityLookUp esrCentral, HashMap<Long, GraphEntity> searchResults2graphEntity) {

        String nodesString = ""; //holds xml tag <node>
        String edgesString = ""; //holds xml tag <edge>

        ArrayList<String> existingEntities = new ArrayList<String>(); //holds unique entity id s
        int nodeLabelKeywordLength = 20; // keywords are added to node label but cannot be too long
        int maxKeywrdsToDisplay = 2;  // max number of keywords to display on node label
        int simplecounter = 0;

        if (esrCentral == null) {
            esrCentral = new EntityLookUp();
        }

        try {
            //String entityAdditionalDataXMLloc = "<entities>";
            for (int i = 0; i < searchResults.size(); i++) {

                EntitySearchResults esr = (EntitySearchResults) searchResults.get(i);
                String nodeLabel = ""; //label for graph node

                //String kwrdsOfEntityDetailsXML = "";
                //ArrayList<String> kwrdsOfEntityDetailsXMLAdded = new ArrayList<String>();

                //avoid duplicates
                //TODO: i<120 should be removed once scoring is implemented (visualDataSourceEntityBased())
                //if (!existingEntities.contains(esr.getEntityID().toString()) && existingEntities.size() < 120) {
                if (!existingEntities.contains(esr.getEntityID().toString())) {

                    existingEntities.add(esr.getEntityID().toString());
                    if (!esr.getEntityFname().isEmpty() || !esr.getEntityFname().equals("null")) {
                        nodeLabel += esr.getEntityFname() + " ";
                    }

                    if (esr.getEntityMname() != null && !esr.getEntityMname().isEmpty() && !esr.getEntityMname().equals("null")) {
                        nodeLabel += esr.getEntityMname() + " ";
                    }

                    if (!esr.getEntitySname().isEmpty() || !esr.getEntitySname().equals("null")) {
                        nodeLabel += esr.getEntitySname();
                    }

                    //double nodeWeight1 = (double) (i + 1) / 100;
                    //double edgeWeight = 0.2 + (double) (i + 1) / 100; //for testing
                    double nodeWeight1 = 0.0; //for testing
                    double edgeWeight = 0.0; //for testing

                    //double edgeWeight = 0.1;

                    if (searchResults2graphEntity != null) {
                        nodeWeight1 = searchResults2graphEntity.get(esr.getEntityID()).getTotalWeight();
                        edgeWeight = searchResults2graphEntity.get(esr.getEntityID()).getTotalWeight();
                        //System.out.println("The weight for "+esr.getEntityID()+" is: "+nodeWeight1);
                    }

                    //set highest weight value
                    //edgeWeight = Math.log10(edgeWeight);
                    //if(esr.getEntityID() == 347) { System.out.println("********* "+edgeWeight); }
                    double edgeWeightOrig = edgeWeight;
                    edgeWeight = Math.log10(edgeWeight + 4.0) - 0.699;
                    if (this.tmpHighestWeight < edgeWeight) {
                        this.tmpHighestWeight = edgeWeight;
                    }

                    //System.out.println("The weight for "+esr.getEntityID()+" is: "+edgeWeight+" and biggest is: "+this.tmpHighestWeight);

                    //Genereate list of keywords to display
                    String thisEntityKwrdAccompList = "";
                    String thisEntityKwrdOnlyList = "\\n[";

                    int keywordCounter = 1;

                    if (displayDetailsKW.containsKey(esr.getEntityID().toString())) {
                        ///////System.out.println("££££ In step 1: ");
                        ArrayList<String> alreadyAddedKw = new ArrayList<String>();

                        for (String accompTitle : displayDetailsKW.get(esr.getEntityID().toString()).keySet()) {

                            thisEntityKwrdAccompList += "&lt;br/&gt;[*" + displayDetailsKW.get(esr.getEntityID().toString()).get(accompTitle) + "]  " + accompTitle + "";
                            if (keywordCounter <= maxKeywrdsToDisplay && !alreadyAddedKw.contains(displayDetailsKW.get(esr.getEntityID().toString()).get(accompTitle))) {
                                alreadyAddedKw.add(displayDetailsKW.get(esr.getEntityID().toString()).get(accompTitle));
                                keywordCounter++;
                                // only displays first 20 chars of a keyword
                                if (displayDetailsKW.get(esr.getEntityID().toString()).get(accompTitle).length() > nodeLabelKeywordLength) {
                                    thisEntityKwrdOnlyList += displayDetailsKW.get(esr.getEntityID().toString()).get(accompTitle).substring(0, nodeLabelKeywordLength) + "...,";
                                } else {
                                    thisEntityKwrdOnlyList += displayDetailsKW.get(esr.getEntityID().toString()).get(accompTitle) + ",";
                                }
                            }

                        }
                    }
                    //remove last , char
                    if (thisEntityKwrdOnlyList.endsWith(",")) {
                        thisEntityKwrdOnlyList = thisEntityKwrdOnlyList.substring(0, thisEntityKwrdOnlyList.length() - 1);
                    }
                    thisEntityKwrdOnlyList += "]";

                    simplecounter++;

                    nodesString += "<node id=\"" + esr.getEntityID().toString() + "\">\\";
                    nodesString += "   <data key=\"id\">" + esr.getEntityID().toString() + "</data>\\";
                    //replace , with \n for display
                    nodesString += " <data key=\"label\">" + "{" + edgeWeightOrig + "} " + nodeLabel.toString().replace("'", "") + thisEntityKwrdOnlyList.replace("'", "").replace(",", "\\n") + "</data>";
                    nodesString += " <data key=\"weight\">" + nodeWeight1 + "</data>\\";

                    if (esr.getEntityEmail() != null) {
                        nodesString += " <data key=\"email\">" + esr.getEntityEmail().replace("'", "") + "</data>\\";
                    } else {
                        nodesString += " <data key=\"email\"> </data>\\";
                    }

                    nodesString += " <data key=\"isActive\">" + esr.getEntityActive() + "</data>\\";

                    nodesString += "</node>\\";

                    //original edgeColor = #F847FF
                    edgesString += ""
                            + "<edge source=\"" + esr.getEntityID().toString() + "\" target=\"001\" id=\"" + esr.getEntityID().toString() + "\">\\"
                            + "   <data key=\"label\"></data>\\"
                            + "   <data key=\"weight\">" + edgeWeight + "</data>\\"
                            + "   <data key=\"edgeColor\">#FFCDE3</data>\\"
                            + "   <data key=\"edgeStyle\">SOLID</data>\\"
                            + "   <data key=\"source\">" + esr.getEntityID().toString() + "</data>\\"
                            + "   <data key=\"target\">001</data>\\"
                            + "   <data key=\"directed\">True</data>\\"
                            + "</edge>\\";

                }
            }

        } catch (NullPointerException npe) {
            //TODO: Add logger
            /////System.out.println("Exception occured while creating visual XML! (visualDataSourceEntityBased)" + npe.toString());
        }

        String visualDataSource = "'\\<graphml>\\"
                + "<key id=\"label\" for=\"all\" attr.name=\"label\" attr.type=\"string\"/>\\"
                + "<key id=\"weight\" for=\"all\" attr.name=\"weight\" attr.type=\"double\"/>\\"
                //+ "<key id=\"width\" for=\"edge\" attr.name=\"width\" attr.type=\"double\"/>\\"
                + "<key id=\"id\" for=\"node\" attr.name=\"id\" attr.type=\"string\"/>\\"
                //+ "<key id=\"address\" for=\"node\" attr.name=\"address\" attr.type=\"string\"/>\\"
                //+ "<key id=\"faculty\" for=\"node\" attr.name=\"faculty\" attr.type=\"string\"/>\\"
                //+ "<key id=\"department\" for=\"node\" attr.name=\"department\" attr.type=\"string\"/>\\"
                + "<key id=\"email\" for=\"node\" attr.name=\"email\" attr.type=\"string\"/>\\"
                //+ "<key id=\"telephone\" for=\"node\" attr.name=\"telephone\" attr.type=\"string\"/>\\"
                + "<key id=\"isActive\" for=\"node\" attr.name=\"isActive\" attr.type=\"bool\"/>\\"
                //+ "<key id=\"matchContent\" for=\"node\" attr.name=\"matchContent\" attr.type=\"string\"/>\\"
                //+ "<key id=\"keywordsList\" for=\"node\" attr.name=\"keywordsList\" attr.type=\"string\"/>\\"
                + "<key id=\"edgeColor\" for=\"edge\" attr.name=\"edgeColor\" attr.type=\"string\"/>\\"
                + "<key id=\"edgeStyle\" for=\"edge\" attr.name=\"edgeStyle\" attr.type=\"string\"/>\\"
                + "<key id=\"source\" for=\"edge\" attr.name=\"source\" attr.type=\"string\"/>\\"
                + "<key id=\"target\" for=\"edge\" attr.name=\"target\" attr.type=\"string\"/>\\"
                + "<key id=\"directed\" for=\"edge\" attr.name=\"directed\" attr.type=\"bool\"/>\\"
                + "<graph edgedefault=\"directed\">\\"
                + "<node id=\"001\">\\"
                + "   <data key=\"id\">001</data>\\"
                + "   <data key=\"label\">" + esrCentral.getEntityFname().replace("'", "") + " " + esrCentral.getEntitySname().replace("'", "") + "</data>\\"
                //+ "   <data key=\"label\">" + searchString.replace("'", "") + "</data>\\"
                + "   <data key=\"weight\">1.0</data>\\"
                //+ "   <data key=\"address\"></data>\\"
                //+ "   <data key=\"faculty\"></data>\\"
                //+ "   <data key=\"department\"></data>\\"
                + "   <data key=\"email\"></data>\\"
                //+ "   <data key=\"telephone\"></data>\\"
                //+ "   <data key=\"matchContent\"></data>\\"
                //+ "   <data key=\"keywordsList\"></data>\\"
                + "   <data key=\"isActive\">True</data>\\"
                + "</node>\\";

        visualDataSource += nodesString;
        visualDataSource += edgesString;

        visualDataSource += "</graph>\\"
                + "</graphml>\\ '";

        //System.out.println("the counter is: " + simplecounter);

        return visualDataSource;
    }

    //TODO: move this to a class
    public String visualDataSourceKwrdBased(List searchResults, String searchString, HashMap<Long, GraphEntity> searchResults2graphEntity) {

        String nodesString = "";
        String edgesString = "";
        ArrayList<String> existingEntities = new ArrayList<String>();
        int simplecounter = 0;

        try {

            for (int i = 0; i < searchResults.size(); i++) {

                EntitySearchResults esr = (EntitySearchResults) searchResults.get(i);
                String nodeLabel = "";

                //avoid duplicates
                //TODO: i<120 should be removed once scoring is implemented (visualDataSourceEntityBased())                
                if (!existingEntities.contains(esr.getEntityID().toString())) {

                    existingEntities.add(esr.getEntityID().toString());
                    if (!esr.getEntityFname().isEmpty() || !esr.getEntityFname().equals("null")) {
                        nodeLabel += esr.getEntityFname() + " ";
                    }

                    if (esr.getEntityMname() != null && !esr.getEntityMname().isEmpty() && !esr.getEntityMname().equals("null")) {
                        nodeLabel += esr.getEntityMname() + " ";
                    }

                    if (!esr.getEntitySname().isEmpty() || !esr.getEntitySname().equals("null")) {
                        nodeLabel += esr.getEntitySname();
                    }

                    double nodeWeight1 = 0.0; //for testing
                    double edgeWeight = 0.0;

                    if (searchResults2graphEntity != null) {
                        nodeWeight1 = searchResults2graphEntity.get(esr.getEntityID()).getTotalWeight();
                        edgeWeight = searchResults2graphEntity.get(esr.getEntityID()).getTotalWeight();
                        //System.out.println("The weight for "+esr.getEntityID()+" is: "+nodeWeight1);
                    }

                    //set highest weight value
                    //edgeWeight = Math.log10(edgeWeight);
                    double edgeWeightOrig = edgeWeight;
                    edgeWeight = Math.log10(edgeWeight + 4.0) - 0.699;
                    if (this.tmpHighestWeight < edgeWeight) {
                        this.tmpHighestWeight = edgeWeight;
                    }

                    //Genereate list of keywords to display
                    //String thisEntityKwrdAccompList = "";
                    String thisEntityKwrdOnlyList = "";

                    simplecounter++;

                    nodesString += "<node id=\"" + esr.getEntityID().toString() + "\">\\";
                    nodesString += "   <data key=\"id\">" + esr.getEntityID().toString() + "</data>\\";
                    //nodesString += " <data key=\"label\">" + nodeLabel.toString().replace("'", "") + thisEntityKwrdOnlyList.replace("'", "") + " {" + edgeWeightOrig + "}" + "</data>";
                    nodesString += " <data key=\"label\">" + "{" + edgeWeightOrig + "} " + nodeLabel.toString().replace("'", "") + thisEntityKwrdOnlyList.replace("'", "").replace(",", "\\n") + "</data>";
                    nodesString += " <data key=\"weight\">" + nodeWeight1 + "</data>\\";
                    if (esr.getEntityEmail() != null) {
                        nodesString += " <data key=\"email\">" + esr.getEntityEmail().replace("'", "") + "</data>\\";
                    } else {
                        nodesString += " <data key=\"email\"> </data>\\";
                    }
                    nodesString += " <data key=\"isActive\">" + esr.getEntityActive() + "</data>\\";
                    nodesString += "</node>\\";

                    edgesString += ""
                            + "<edge source=\"" + esr.getEntityID().toString() + "\" target=\"001\" id=\"" + esr.getEntityID().toString() + "\">\\"
                            + "   <data key=\"label\"></data>\\"
                            + "   <data key=\"weight\">" + edgeWeight + "</data>\\"
                            + "   <data key=\"edgeColor\">#FFCDE3</data>\\"
                            + "   <data key=\"edgeStyle\">SOLID</data>\\"
                            + "   <data key=\"source\">" + esr.getEntityID().toString() + "</data>\\"
                            + "   <data key=\"target\">001</data>\\"
                            + "   <data key=\"directed\">True</data>\\"
                            + "</edge>\\";

                }
            }

        } catch (NullPointerException npe) {
            //TODO: Add logger
            /////System.out.println("Exception occured while creating visual XML! (visualDataSourceEntityBased)" + npe.toString());
        }

        String visualDataSource = "'\\<graphml>\\"
                + "<key id=\"label\" for=\"all\" attr.name=\"label\" attr.type=\"string\"/>\\"
                + "<key id=\"weight\" for=\"all\" attr.name=\"weight\" attr.type=\"double\"/>\\"
                + "<key id=\"id\" for=\"node\" attr.name=\"id\" attr.type=\"string\"/>\\"
                + "<key id=\"email\" for=\"node\" attr.name=\"email\" attr.type=\"string\"/>\\"
                + "<key id=\"isActive\" for=\"node\" attr.name=\"isActive\" attr.type=\"bool\"/>\\"
                + "<key id=\"edgeColor\" for=\"edge\" attr.name=\"edgeColor\" attr.type=\"string\"/>\\"
                + "<key id=\"edgeStyle\" for=\"edge\" attr.name=\"edgeStyle\" attr.type=\"string\"/>\\"
                + "<key id=\"source\" for=\"edge\" attr.name=\"source\" attr.type=\"string\"/>\\"
                + "<key id=\"target\" for=\"edge\" attr.name=\"target\" attr.type=\"string\"/>\\"
                + "<key id=\"directed\" for=\"edge\" attr.name=\"directed\" attr.type=\"bool\"/>\\"
                + "<graph edgedefault=\"directed\">\\"
                + "<node id=\"001\">\\"
                + "   <data key=\"id\">001</data>\\"
                + "   <data key=\"label\">" + searchString.replace("'", "") + "</data>\\"
                + "   <data key=\"weight\">1.0</data>\\"
                + "   <data key=\"email\"></data>\\"
                + "   <data key=\"isActive\">True</data>\\"
                + "</node>\\";

        visualDataSource += nodesString;
        visualDataSource += edgesString;

        visualDataSource += "</graph>\\"
                + "</graphml>\\ '";

        //System.out.println("the counter is: " + simplecounter);

        return visualDataSource;
    }

    /*
    //TODO: Fix this in new class.  Need to have a global var + getter & setter
    private String getEntityAdditionalDataXML() {
    return this.entityAdditionalDataXML;
    }
    //TODO: Fix this in new class.  Need to have a global var + getter & setter

    private void setEntityAdditionalDataXML(String stringXML) {
    this.entityAdditionalDataXML = stringXML;
    }
     */
}
